Re: Load hash from a scalar



scooter wrote:
I want to load the value from scalar variable to a hash

Example:
my $a = "a, 1, b, 2, c, 3";

Now, I need to create a hash %b, which holds values like
%b = { a => 1, b => 2, c => 3 };

$ perl -le'
use Data::Dumper;
my $a = "a, 1, b, 2, c, 3";
my %b = $a =~ /[^, ]+/g;
print Dumper \%b;
'
$VAR1 = {
'c' => '3',
'a' => '1',
'b' => '2'
};


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: Substituting in a group
    ... And avoid using the substitution operator or capturing parentheses. ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (comp.lang.perl.misc)
  • Re: ISALPHA
    ... The script does not give error message but it doesn't ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (comp.lang.perl.misc)
  • Re: how to find how many items are in a reference?
    ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (comp.lang.perl.misc)
  • Re: Removing files but maintaining last 15
    ... but maintaining the last 15 *.trw files... ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (comp.unix.shell)
  • Re: how to implement this with perl
    ... use warnings; ... John ... Perl isn't a toolbox, but a small machine shop where you ...
    (perl.beginners)