Re: Count Recurrence of Paired Values in Text File
- From: "gf" <greg.ferguson@xxxxxxxxxxxxx>
- Date: 26 Feb 2007 21:48:13 -0800
On Feb 26, 5:48 pm, "jim" <jimmorga...@xxxxxxxxx> wrote:
Hi,
I'm trying to write what should be a simple script, but which I keep
getting hung up on. I've scoured past posts, but still haven't found
the answer I'm looking for.
I have a text file that will re-populate on a weekly basis. I want to
read this file into a hash and return a count of unique instances
based on an index of columns.
More specifically, of the 15 tab-delimited columns in the spread***,
I would like to get a count for each recurrence of the column 1, 4, 13
combination (area + user + manager). The remaining fields contain
unique data (e.g. order numbers, etc.) that I'm not interested in for
this view.
I'd go after it this way. (Code is untested)...
#!/usr/bin/perl
# always use warnings and strict
use warnings;
use strict;
# I always pre-define my array and hashes as a visual reminder they're
empty.
# It is always good to make your code be as self-documenting as
possible.
my %counts = ();
# Always use the three parameter version of open().
# This tests open for a successful open...
if ( open( my $FILE, '<', 'data.txt' ) ) {
while (<$FILE>) {
# Get the columns we want. Remember Perl is usually 0-indexed.
my @cols = ( split( "\t", $_ ) )[ 0, 3, 12 ];
# "@cols" uses space separated column data for readability
later.
$counts{"@cols"}++;
}
close $FILE;
# loop through the keys...
foreach (
# descending sort on count...
sort {
( $counts{$b} <=> $counts{$a} )
# ...or ascending on keys.
or ( $a cmp $b )
}
keys %counts
)
{
# print the count and the key
print $counts{$_}, "\t", $_, "\n";
}
}
.
- References:
- Prev by Date: GD and png question
- Next by Date: extract data from array
- Previous by thread: Count Recurrence of Paired Values in Text File
- Next by thread: cgi calander
- Index(es):