Gossamer Forum
Home : General : Perl Programming :

Help me! can we do this??

Quote Reply
Help me! can we do this??
Hi all,

I am learning perl , I just wonder if I have a flat file.

record.txt
Code:
Toyota|California|||||
Toyota|California|||||
Toyota|Arizona|||||
Toyota|Nevada|||||
Toyota|Florida|||||
Honda|California|||||
Honda|Florida|||||
Honda|Arizona|||||

How we write a code to have a result is:

Code:
MADE CALIFORINA ARIZONA NEVADA FLORIDA TOTAL
==== ========== ======= ====== ======= =====
Toyota 2 1 1 1 5
Honda 1 1 0 1 3


I can only do:

MADE TOTAL
==== =====
Toyota 5
Honda 3
Here are some of my code:
Code:
#!/opt/WWW/tools/perl5/perl
#summarize.cgi

open (DATA,"record.txt") || die ("Can't Open data File \n");
@data=<DATA>;
close DATA;

foreach $line (@data) {


($made, $state, $tmp , $tmp, $tmp, $tmp)=split(/\|/,$line);

if ($made) {

$count{$made}++;
}

if ($state) {

$countstate{$state}++;
}

}


&header_response;

$x=0;

foreach $made (keys %count) {
$x++;

print "<TR ><TD>$x</TD><TD><b>$made&nbsp;</b></TD><TD>$count{$made}&nbsp;</TD></TR> \n";

}
&close_response;


&log_response;
$z=0;
foreach $LogOp (keys %countstate) {
$z++;

print "<TR ><TD>$z</TD><TD><b>$state&nbsp;</b></TD><TD>$countstate{$state}&nbsp;</TD></TR> \n";

}

&footer_response;

Can you help me to solve this problem ?

Thank you very much,

TD.
Quote Reply
Re: [britneythuyduong] Help me! can we do this?? In reply to
Hi,

You may want to try the code below:

Code:
my $hash;
foreach $line (@data) {
($made, $state) = split(/\|/ => $line);
$hash->{$made}->{$state} += 1;
}
foreach $made (keys %$hash) {
foreach $state (keys %{$hash->{$made}}) {
$hash->{$made}->{Total} += $hash->{$made}->{$state};
}
}


the $hash will contain:

Code:
$VAR1 = {
'Toyota' => {
'California' => 2,
'Florida' => 1,
'Total' => 5,
'Arizona' => 1,
'Nevada' => 1
},
'Honda' => {
'California' => 1,
'Florida' => 1,
'Total' => 3,
'Arizona' => 1
}
};


Cheers,
Jean
Gossamer Threads, Inc.
Quote Reply
Re: [jean] Help me! can we do this?? In reply to
wonderful split but

not so understand this

Code:


($made, $state) = split(/\|/ => $line);
Quote Reply
Re: [golden_water] Help me! can we do this?? In reply to
>>
($made, $state) = split(/\|/ => $line);
<<

That is splitting your file delimiters eg...

Toyota|Florida|||||||

$made will be Toyota or whatever and $state will be Florida, although you could probably use:

($made, $state) = split /\|/, $line, 3;

....to limit the split.

Last edited by:

Paul: Sep 11, 2002, 2:33 AM
Quote Reply
Re: [Paul] Help me! can we do this?? In reply to
this is easy understand

($made, $state) = split /\|/, $line, 3;


But this

split(/\|/ => $line);

especially a ' =>' a new feature???
Quote Reply
Re: [golden_water] Help me! can we do this?? In reply to
=> is just a fancy way to write a comma. The advantage usually is that you don't need to quote what stands on the left side of it.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Help me! can we do this?? In reply to
thanks....
Quote Reply
Re: [jean] Help me! can we do this?? In reply to
 
Hi Jean,

The problem is this flat file keep update ( more records are added in this flat file). So we really don't know how much California, Florida ...Blush.

It have to count itself every time we open this .cgi file.
I have a flu now, so it's blank and feel dizzy with the code
Blush