For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > Load hash from a scalar









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Load hash from a scalar
Scooter

2008-03-28, 4:13 am

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 };

TIA.

John W. Krahn

2008-03-28, 4:13 am

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
Sanket Vaidya

2008-03-31, 4:42 am


Here is the code to accomplish your task.


use warnings;
use strict;

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

$a =~ s/ //g; #delete spaces from $a

my @array = split(",",$a);

my %b = (
$array[0] => $array[1],
$array[2] => $array[3],
$array[4] => $array[5]
);

for (keys%b)
{
print "$_ => $b{$_}\n";
}

The output is:

c => 3
a => 1
b => 2

Hope it is useful.


-----Original Message-----
From: scooter [mailto:verma.abhinav@gmail.com]
Sent: Friday, March 28, 2008 12:00 PM
To: beginners@perl.org
Subject: Load hash from a scalar

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 };

TIA.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

John W. Krahn

2008-03-31, 4:43 am

sanket vaidya wrote:
>
>
> Here is the code to accomplish your task.
>
>
> use warnings;
> use strict;
>
> my $a = "a, 1, b, 2, c, 3";
>
> $a =~ s/ //g; #delete spaces from $a


You can do that more efficiently with tr///:

$a =~ tr/ //d; #delete spaces from $a


> my @array = split(",",$a);


Or you can delete the spaces when you split the string:

my @array = split / *, */, $a;


> my %b = (
> $array[0] => $array[1],
> $array[2] => $array[3],
> $array[4] => $array[5]
> );


You can just assign a list directly to a hash:

my %b = @array;

Or just assign the list returned from split:

my %b = split / *, */, $a;


> for (keys%b)
> {
> print "$_ => $b{$_}\n";
> }
>
> The output is:
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com