RE: Load hash from a scalar
- From: sanket.vaidya@xxxxxxxxx (Sanket Vaidya)
- Date: Mon, 31 Mar 2008 11:23:44 +0530
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@xxxxxxxxx]
Sent: Friday, March 28, 2008 12:00 PM
To: beginners@xxxxxxxx
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@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
.
- Follow-Ups:
- Re: Load hash from a scalar
- From: John W. Krahn
- Re: Load hash from a scalar
- References:
- Load hash from a scalar
- From: Scooter
- Load hash from a scalar
- Prev by Date: Re: need to find all the directories which have soft links in it
- Next by Date: perl byte code generation
- Previous by thread: Re: Load hash from a scalar
- Next by thread: Re: Load hash from a scalar
- Index(es):
Relevant Pages
|