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

Hash obtained from excel file saved in CSV format. Need to obtain output based on hash keys and corresponding values. How to read contents of various CSV file with data stored in various sheets? Now, I'm able to read data from sheet1 and store in a hash.

Contents of CSV sheet1, stored in hash table.

$VAR1 = 'key_2'; $VAR2 = 'value_2'; $VAR3 = 'key_1'; $VAR4 = 'value_1';

Required output should be to print, contents of csv are value_1 and value_2 in a single print statement

Please help

Replies are listed 'Best First'.
Re: keys and values
by LanX (Saint) on Sep 22, 2014 at 20:43 UTC
    > Required output should look like this.  >  Print, contents of csv are value_1 and value_2

     print "contents of csv are $hash{key_1} and $hash{key_2}"

    you'll most probably need to have a look at How (Not) To Ask A Question

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    And next time you dump your hash better use a reference \%hash and not just %hash . :)

Re: keys and values
by shmem (Chancellor) on Sep 22, 2014 at 22:44 UTC

    Please read perldata.

    Contents of CSV sheet1, stored in hash table.
    $VAR1 = 'key_2'; $VAR2 = 'value_2'; $VAR3 = 'key_1'; $VAR4 = 'value_1';

    This is not a hash table. This is a series of assignments of values to four independent variables. At most, this looks like an array broken up into pieces, which should be written as

    @VAR = ('key_2', 'value_2', 'key_1', 'value_1');

    In a perl hash table, values are associated to keys, and the values can be retrieved with the keys.

    %VAR = ( key_1 => 'value_1', key_2 => 'value_2'); print "value for key 2: '$VAR{key_2}'\n"; __END__ value for key 2: 'value_2'

    Please read perldata. Done that (and trying what's stated there) you will be able to find out how to print values corresponding to keys.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      This is not a hash table. This is a series of assignments of values to four independent variables. At most, this looks like an array broken up into pieces ...

      It's most likely this:

      use Data::Dumper; my %hash = ( key_1 => 'value_1', key_2 => 'value_2' ); print Dumper(%hash); __END__ $VAR1 = 'key_2'; $VAR2 = 'value_2'; $VAR3 = 'key_1'; $VAR4 = 'value_1';

      Better would have been print Dumper(\%hash);

Re: keys and values
by Anonymous Monk on Sep 22, 2014 at 20:50 UTC
    How to read contents of various CSV file with data stored in various sheets?

    Text::CSV

    Required output should be to print, contents of csv are value_1 and value_2 in a single print statement

    Assuming your hash is named %hash, how about print "$hash{key_1} $hash{key_2}\n";

Re: keys and values
by jellisii2 (Hermit) on Sep 23, 2014 at 11:51 UTC
    If you define "sheet" like it is in Excel, it doesn't exist in a CSV is my understanding.