SPUG: hash from string
Ron Hartikka
ronh at iainc.com
Wed Jul 12 15:18:24 CDT 2000
# Hope this helps.
# This code
$x = "
'xyz' = '/export/home' 'adfd' = '/usr/there'
'ddddd' = '/someplace/else'
";
while ($x =~ /'(.*?)' = '(.*?)'/g) {
$hash{$1} = $2;
print "\$hash{$1} = $2 = $hash{$1}\n";
}
__END__
# has this output
$hash{xyz} = /export/home = /export/home
$hash{adfd} = /usr/there = /usr/there
$hash{ddddd} = /someplace/else = /someplace/else
# (on my machine).
# As for map, it wants a list and you have a scalar (the string).
# Depending on details of your actual input you may need to make the regular
expression smarter.
# For example, maybe you need to put \s* around the =, etc.
# Note that the regular expression does not use the newlines; I even took
one out.
Ron Hartikka
-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
Peter Dueber
Sent: Wednesday, July 12, 2000 2:28 PM
To: spug-list at pm.org
Subject: SPUG: hash from string
Anyone have a good technique (maybe using map?) to take a string (with
embedded new-lines)
like the following:
$x = "
'xyz' = '/export/home'
'adfd' = '/usr/there'
'ddddd = '/someplace/else'
etc";
and directly convert it to a hash equivalent to :
%hash = ('xyz' => '/export/home',
'adfd' => '/usr/there',
'ddddd => '/someplace/else',
etc.
);
Lost in map space.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list