SPUG: hash from string
Matt Tucker
tuck at whistlingfish.net
Wed Jul 12 20:10:13 CDT 2000
It's funny how some of the simplest problems generate the most traffic.
-- Peter Dueber <peter at hivnet.fhcrc.org> spake thusly:
> $x =~ s/^\s+|\s+$|'//g;
> %hash = split /\n|\s*=\s*/, $x;
>
> Now, how can I get it into _one_ line? The following returns nothing:
>
> %hash = split /\n|\s*=\s*/, $x =~ s/^\s+|\s+$|'//g;
The trouble is that $x =~ s/...//. returns the number of substitutions
rather than $x itself, so you can't split it.
To have it on one line you could do:
%hash = split /\n|\s*=\s*/, ($x =~ s/^\s+|\s+$|'//mg, $x);
although I think that's kind of silly.
Also, note that the switches to s/// should be /mg rather than just /g.
Otherwise you can end up with spaces at the beginning of all lines but
the first.
- Matt
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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