Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

counting unique elements of an array

by mmittiga17 (Scribe)
on Jan 30, 2008 at 17:15 UTC ( [id://665168]=perlquestion: print w/replies, xml ) Need Help??

mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am struggling to create a report that will parse a bunch of files 1 at time and report back the number transactions per unique bank code. Bank code is the first field in the separated my \t in each file. Here is the code I have which is not counting correctly. =====
open (logfile, ">>$LogName"); while ($line = <cfile>) { $counter++; $BankCode=(split(/\t/,$line))[0]; print "$BankCode\n"; push(@bc ,"$BankCode"); } my %count; map { $count{$_}++ } @bc; $count = return %count; map {print logfile map {print logfile "$ClientCode[$clientindex][0]\ +t$_\t$FileType[$fileindex][1]\t$CurrFile\t${count{$_}}\t$CurrZipFile\ +t$yyyymm\t$onDemand\n"} sort keys(%count); ===== $_ = the BankCode in the print line
Any suggestions? It appears to add the count from the last unique bankcode from the last file to the new files bankcode count. Thanks in advance

Replies are listed 'Best First'.
Re: counting unique elements of an array
by toolic (Bishop) on Jan 30, 2008 at 17:32 UTC
    Not sure why yours isn't working, but is this what you're looking for?
    #!/usr/bin/env perl use warnings; use strict; my @bc; my %count; while (<DATA>) { my $BankCode = (split /\t/)[0]; $count{$BankCode}++; } for (keys %count) { print "$_ $count{$_}\n" } __DATA__ code1 fooz barz code2 fooz barz code1 fooz barz code3 fooz barz code1 fooz barz code1 fooz barz

    prints:

    code3 1 code2 1 code1 4
Re: counting unique elements of an array
by moritz (Cardinal) on Jan 30, 2008 at 17:30 UTC
    $count = return %count;

    Maybe you meant: my $count = scalar keys %count;

    It appears to add the count from the last unique bankcode from the last file to the new files bankcode count

    That sounds like a scoping issue. Are you using strict and warnings? You should be.

    And it's impossible to tell what went wrong without seeing the whole of the loop. Including the places where the variables are defined for the first time.

    BTW your map in void context looks a bit scary - your intention might be clearer if you used a for loop. If you like short code, you can also write @count{@bc} = undef; instead ;-)

Re: counting unique elements of an array
by apl (Monsignor) on Jan 30, 2008 at 17:59 UTC
    toolic gave you a great solution. I just wanted to mention a few minor things:

    Internal variable $. could be used instead of $counter. Internal variable $_ could be used in lieu of $line. The print isn't needed after you determine the split worked properly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://665168]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found