
FishMonger
Veteran
/ Moderator
Aug 23, 2015, 2:01 PM
Post #5 of 10
(2994 views)
|
Re: [stuckinarut] Flagging list data hybrid situation
[In reply to]
|
Can't Post
|
|
Here's one possible way to build the hash.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %fruit; foreach my $list ( qw|D I L| ) { my $file = "list$list.txt"; open my $fh, '<', $file or die "failed to open '$file' <$!>"; while (my $fruit = <$fh>) { $fruit =~ s/\s+$//; $fruit{$fruit}{total_cnt}++; $fruit{$fruit}{$list}++; push @{ $fruit{$fruit}{list} }, $list; } close $fh; } print Dumper \%fruit; Which outputs:
$VAR1 = { 'peach' => { 'I' => 1, 'total_cnt' => 2, 'L' => 1, 'list' => [ 'I', 'L' ] }, 'apple' => { 'I' => 1, 'total_cnt' => 1, 'list' => [ 'I' ] }, 'pear' => { 'D' => 1, 'I' => 1, 'total_cnt' => 2, 'list' => [ 'D', 'I' ] }, 'grape' => { 'D' => 1, 'I' => 1, 'total_cnt' => 2, 'list' => [ 'D', 'I' ] } }; Since I can't determine which example output of yours you really want, I can't show you how to output the data in hash other than a simple dump to show the structure.
|