Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a "simple" report 1

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
All,

This little exercise has me confused. I would like to read this file:

AL|2
AL|4
AL|7
AL|10
AL|11
AK|2
AK|4
AK|7
AK|10
AK|11
AZ|2
AZ|4
AZ|7
AZ|10
AZ|11
AR|2
AR|4
AR|7
AR|10
AR|11
CA|2
CA|4
CA|7
CA|10
CA|11

And create a report that looks like:

2 4 7 10 11
AK 1 1 1 1 1
AL 1 1 1 1 1
AZ 1 1 1 1 1
AR 1 1 1 1 1
CA 1 1 1 1 1

So, a count AK records with a flag of 2 is 1 and so on.

Make sense?
 
what have you tried so far?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
is this supposed to be in some kind of order?

AK
AL
AZ
AR
CA

because that is not alpha order or original order of file


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
well, the order can easily be adjusted, this keeps original file order:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%HoH[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]%col[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@order[/blue] = [red]([/red][red])[/red][red];[/red]
[red]{[/red]
[url=http://perldoc.perl.org/functions/no.html][black][b]no[/b][/black][/url] [green]warnings[/green][red];[/red]
   [olive][b]while[/b][/olive][red]([/red][black][b]my[/b][/black] [blue]$line[/blue] = <DATA>[red])[/red][red]{[/red]
      [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]$line[/blue][red];[/red]
      [black][b]my[/b][/black][red]([/red][blue]$k[/blue],[blue]$v[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\|[/b][/purple][/purple][red]/[/red],[blue]$line[/blue][red])[/red][red];[/red]
      [blue]$col[/blue][red]{[/red][blue]$v[/blue][red]}[/red]++[red];[/red]
      [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@order[/blue],[blue]$k[/blue] [olive][b]if[/b][/olive] [red]([/red]![url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$HoH[/blue][red]{[/red][blue]$k[/blue][red]}[/red][red])[/red][red];[/red]
      [blue]$HoH[/blue][red]{[/red][blue]$k[/blue][red]}[/red][red]{[/red][blue]$v[/blue][red]}[/red]++[red];[/red]
 
    
   [red]}[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red][blue]$a[/blue] <=> [blue]$b[/blue][red]}[/red] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%col[/blue][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$keys[/blue] [red]([/red][blue]@order[/blue][red])[/red] [red]{[/red]
      [black][b]print[/b][/black] [red]"[/red][purple][blue]$keys[/blue][purple][b]\t[/b][/purple][/purple][red]"[/red], [black][b]join[/b][/black][red]([/red][red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [black][b]sort[/b][/black] [red]{[/red][blue]$a[/blue] <=> [blue]$b[/blue][red]}[/red] [url=http://perldoc.perl.org/functions/values.html][black][b]values[/b][/black][/url] [blue]%[/blue][red]{[/red][blue]$HoH[/blue][red]{[/red][blue]$keys[/blue][red]}[/red][red]}[/red][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
  [red]}[/red]
[red]}[/red]
[teal]__DATA__[/teal]
[teal]AL|2[/teal]
[teal]AL|4[/teal]
[teal]AL|7[/teal]
[teal]AL|10[/teal]
[teal]AL|11[/teal]
[teal]AK|2[/teal]
[teal]AK|4[/teal]
[teal]AK|7[/teal]
[teal]AK|10[/teal]
[teal]AK|11[/teal]
[teal]AZ|2[/teal]
[teal]AZ|4[/teal]
[teal]AZ|7[/teal]
[teal]AZ|10[/teal]
[teal]AZ|11[/teal]
[teal]AR|2[/teal]
[teal]AR|4[/teal]
[teal]AR|7[/teal]
[teal]AR|10[/teal]
[teal]AR|11[/teal]
[teal]CA|2[/teal]
[teal]CA|4[/teal]
[teal]CA|7[/teal]
[teal]CA|10[/teal]
[teal]CA|11[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

output:

Code:
	2	4	7	10	11
AL	1	1	1	1	1
AK	1	1	1	1	1
AZ	1	1	1	1	1
AR	1	1	1	1	1
CA	1	1	1	1	1

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, I shut it down last night after i posted this topic...was frustrated and needed to step away for a bit. I was so close this morning, then read your post. It's great how you make these look so simple and your willingness to help folks out.

Thanks again.
 
just more more...

If we have this data:

__DATA__
AL|7
AL|7
AL|7
AL|10
AL|10
AK|2
AK|2
AK|2
AK|10
AK|10
AZ|11
AZ|11
AZ|11
AZ|11
AZ|11

Report should look like:
2 4 7 10 11
AL 0 0 3 2 0
AK 3 0 0 2 0
AZ 0 0 0 0 5

counts need to be sumed. i'm looking at this now, just wanted to pass it your way...i'm sure you'll beat me to the answer :)
 
maintain a hash of values as they come in, and prior to printing check if your 2d matrix is complete, and if not populate the holes with 0 if the element doesn't exist

Code:
  if (!exist ($HoH{$key}{$value}) {
     $HoH{$key}{$value}=0;
  }

HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
the code I posted does do the total summing already:

$HoH{$k}{$v}++;

but if you need 0's that would have to be initialized like Paul showed.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
if the cols are always 2 4 7 10 11 this could be done "easier" [wink]

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%HoH[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@col[/blue] = [red]qw([/red][purple]2 4 7 10 11[/purple][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@order[/blue] = [red]([/red][red])[/red][red];[/red]
[red]{[/red]
[url=http://perldoc.perl.org/functions/no.html][black][b]no[/b][/black][/url] [green]warnings[/green][red];[/red]
   [olive][b]while[/b][/olive][red]([/red][black][b]my[/b][/black] [blue]$line[/blue] = <DATA>[red])[/red][red]{[/red]
      [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]$line[/blue][red];[/red]
      [black][b]my[/b][/black][red]([/red][blue]$k[/blue],[blue]$v[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\|[/b][/purple][/purple][red]/[/red],[blue]$line[/blue][red])[/red][red];[/red]
      [olive][b]unless[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$HoH[/blue][red]{[/red][blue]$k[/blue][red]}[/red][red])[/red] [red]{[/red]
         [blue]$HoH[/blue][red]{[/red][blue]$k[/blue][red]}[/red] = [red]{[/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue] => [red]'[/red][purple]0[/purple][red]'[/red][red]}[/red] [blue]@col[/blue][red]}[/red][red];[/red]
         [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@order[/blue],[blue]$k[/blue][red];[/red]
      [red]}[/red]
      [blue]$HoH[/blue][red]{[/red][blue]$k[/blue][red]}[/red][red]{[/red][blue]$v[/blue][red]}[/red]++[red];[/red]
   [red]}[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [blue]@col[/blue][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$keys[/blue] [red]([/red][blue]@order[/blue][red])[/red] [red]{[/red]
      [black][b]print[/b][/black] [red]"[/red][purple][blue]$keys[/blue][purple][b]\t[/b][/purple][/purple][red]"[/red],[black][b]join[/b][/black][red]([/red][red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [black][b]map[/b][/black] [red]{[/red][blue]$HoH[/blue][red]{[/red][blue]$keys[/blue][red]}[/red][red]{[/red][blue]$_[/blue][red]}[/red][red]}[/red] [blue]@col[/blue][red])[/red],[red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]AL|7[/teal]
[teal]AL|7[/teal]
[teal]AL|7[/teal]
[teal]AL|10[/teal]
[teal]AL|10[/teal]
[teal]AK|2[/teal]
[teal]AK|2[/teal]
[teal]AK|2[/teal]
[teal]AK|10[/teal]
[teal]AK|10[/teal]
[teal]AZ|11[/teal]
[teal]AZ|11[/teal]
[teal]AZ|11[/teal]
[teal]AZ|11[/teal]
[teal]AZ|11[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

but if you don't know what columns there are before hand.... never mind [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks again Kevin and Paul for the solution. Very well explained too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top