Output:use strict; use warnings; use Data::Dumper; my @colNames; my %all; # Collect server info while (<DATA>){ my @col = split /,/; for my $c(@col){ $c =~ s/^\s+//; # Strip Leading white space $c =~ s/\s+$//; # Strip trailing white space } if ($col[0] eq "id"){ @colNames = @col; next; } my %info; @info{@colNames} = @col; # Fill the hash, using slice next unless $info{id} ; # ignore lines without valid ID $all{ $info{id} } = [@col]; # Save each row } # Print server info Print_Server_Info (21); Print_Server_Info (22); my $l = find_by_kwd ("post", "perl programmer"); print "Programmer is ID# $l\n"; #--end of program -- sub Print_Server_Info{ my $id = shift @_; my %info; @info{@colNames} = @{ $all{$id} }; # Fill the hash, using slice + print "Info for ID=$id:\n" . Dumper \%info; } sub find_by_kwd{ my ($name, $value)=@_; for my $id (keys %all){ my %info; @info{@colNames} = @{ $all{$id} }; # Fill the hash, using slic +e return $id if $info{ $name } eq $value; } return undef; # Not found } __DATA__ id ,name,sal,post,income,country ,address..$#number_of_columns 21,hhdh,23,lawyer,0,US,GA,XYZ...$#number_of_rows 22,shjhds,24,perl programmer,999999999999,US,TX,XYZ..$#number_of_rows
Update: Added FIND code.Info for ID=21: $VAR1 = { 'country' => 'US', 'sal' => '23', 'post' => 'lawyer', 'name' => 'hhdh', 'id' => '21', 'income' => '0', 'address..$#number_of_columns' => 'GA' }; Info for ID=22: $VAR1 = { 'country' => 'US', 'sal' => '24', 'post' => 'perl programmer', 'name' => 'shjhds', 'id' => '22', 'income' => '999999999999', 'address..$#number_of_columns' => 'TX' }; Programmer is ID# 22
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
In reply to Re: hash of hash
by NetWallah
in thread hash of hash
by luckysing
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |