SPUG: hashes, arrays, map, join
John Cokos, CEO, iWeb, Inc.
jcokos at iwebsys.com
Fri Mar 9 18:53:06 CST 2001
I've been knocking my head agains the wall all day
on this stupid little problem ... somebody please
bail me out.
Assume a string like this.
my $results = "12345:15,32984732:25,733263:12,349874:5";
I want to place it into a hash, keyed by the first number,
valued by the second one. That's not a problem.
my %ids = ();
%ids = map { split(/\:/) } split(/\,/,$results);
Now, I need to add (or delete) some of these keys.
delete $ids{12345};
$ids{5555} = 12;
Again, no problem, that works.
The pickle, is getting this updated hash back into the comma
separated format that it started with. Totally lost here.
This just flattens it.
print join(",", %ids);
This works, but it's hideous (gotta be a smoother way). I can
see this being a pretty intense maneuver on a large set:
my %set = ();
my @array;
foreach my $key ( keys %ids) { push @array, "$key:$ids{$key}"; }
print join(",", @array);
Note, that in real life, that string ($results, above) may have upwards
of 100,000 comma separated values, so something efficient is pretty
important.
Appreciate your advice, everyone.
John
========================================
John Cokos, President / CEO: iWeb Inc.
http://www.iwebsys.com
jcokos at ccs.net
========================================
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list