in reply to Re: question:File to hash using map
in thread question:File to hash using map

This does not work!

Data dumper returns:
$VAR1 = 'A'; $VAR2 = 'B,C'; $VAR3 = 'D'; $VAR4 = 'E,F'; $VAR5 = 'G'; $VAR6 = 'H,I';

Replies are listed 'Best First'.
Re^3: question:File to hash using map
by liverpole (Monsignor) on Jan 30, 2007 at 17:12 UTC
    Hi narashima,

    Zaxo's solution actually does work.  I think you've made a mistake in the way you pass the data to Data::Dumper.

    You need to pass a reference to the hash, not just the hash itself.

    Try this:

    use strict; use warnings; use Data::Dumper; # Pretend that I've already read the file into @lines ... my @lines = ( 'A, B, C', 'D,E,F', 'G, H,I', ); # Zaxo's solution my %hash = map { chomp; split ',', $_, 2 } @lines; # View the data (use a *reference* to the hash, though!) printf "Data => %s\n", Dumper(\%hash);

    which correctly outputs the following...

    Data => $VAR1 = { 'A' => ' B, C', 'D' => 'E,F', 'G' => ' H,I' };

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks for pointing out the silly error Liverpole