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 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: store array as a hash
by Corion (Patriarch) on Jun 29, 2010 at 08:19 UTC

    I think your usage of reading line-by-line from $abc is not what the documentation of Text::CSV recommends:

    ... while ( my $row = $csv->getline( $fh ) ) { ...

    For the matter at hand, you seem to want to do queries by (only) the server id, and then output the row associated with that server? The easiest way is to load all your data into a hash then and use the server id as the hash key:

    my %server_info; while ( my $row = $csv->getline( $abc ) ) { my @columns = $csv->fields(); my $server_id = $columns[0]; $server_info{ $server_id } = \@columns; } my $query = $ARGV[0]; print "Searching for server id '$query':"; use Data::Dumper; my $info = $server_info{ $query } || 'No results found'; print Dumper $info;

      Tx for the help monks:P

Re: store array as a hash
by Jenda (Abbot) on Jun 29, 2010 at 08:30 UTC

    Declare the hash outside the loop and then store the row as: $hashname{$columns[0]} = \@columns;.

    Though that will really only let you check for the existence of a key and get the data for that key. Queries based on other columns will not be possible, getting the rows sorted by the ID neither, ... If you need anything more complicated than "give me the data for ID ...", use DBD::CVS/DBD::AnyData or import the data into DBD::SQLite.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.