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

I am using the perl Text::csv perl module and IO::Scalar module to get csv data the data is as follows:

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 """""..... "......

i cannot find how i can create a hash record so that i can do a search on "ANY" key from the id col which has multiple values associated(name,sal,post,income,country ,address..$#number_of_columns) so as to obtain all values from any single row of record based on key (id) ---------------------------------------------------------

my $url1="http://xyz.com"; my $content = LWP::Simple::get($url1); my $abc= new IO::Scalar \$content; my $csv = Text::CSV->new(); my $data; while(<$abc>){ if ($csv->parse($_)) { next if (/#(.*?)somedata/);#skip the first line my @columns = $csv->fields();#get the coulmns and the +corresponding row value which are stored in each index of array print "Server id: $columns[0]\n\t Server_Name: $column +s[7]\n";#index 0 stores 21,22 after skipping column names similarly i +ndex 2 stores 23,24 #my %ass=($columns[0],$columns[2])#prints 21=> +23|22=>24 #print "@col1";# <code>#following is changed code $server_id= $columns[6]; $server_info{$server_id}=\@columns; #print $server_info{"'someno'"}; $query = "007"; use Data::Dumper; $info = $server_info{ "$query" } ; print Dumper $info; } }
--------------------------------------------------------------- </code>

code is running fine now what i want to do is get name value pairs for example id |state 2146_oak=>state::GA:;US How do i find single data based on key from the entire hash now i am able to get values for entire row based on key i just want a single value from the entire hash based on the row key. Also suppose we have a value called state =state::GA:;US .I want to do search on the vale and if i find keyword GA in the value string i need to use the key 2146_oak and use it for some purpose .how do i do pattern matching on this. Thnx for the help i am a beginner:) soon will become a Perl monk yipee!!

Replies are listed 'Best First'.
Re: hash of hash
by NetWallah (Canon) on Jul 02, 2010 at 06:05 UTC
    Here is some working code to get you started:
    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
    Output:
    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
    Update: Added FIND code.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis