SPUG: hashes, arrays, map, join

Bill Alford billa at willapabay.org
Mon Mar 12 15:11:35 CST 2001


I forget where I saw this, but if you are going over a large map, using a
loop and each() is fastest and/or most efficient technique.  You don't
have to make a temporary array with every key and you don't have all the
lookups.

while (($key, $value) = each(%ids)) {
  $string .= "$key:$value,";
}
$string =~ s/,$//;

Bill

On Fri, 9 Mar 2001, Alex Leites wrote:

> What's wrong with:
> 
> join ',', map( "$_:$ids{$_}", keys %ids
> 
> Do you care about the order?
> 
> Alex
> 
> -----Original Message-----
> From: John Cokos, CEO, iWeb, Inc. [mailto:jcokos at iwebsys.com]
> Sent: Friday, March 09, 2001 5:11 PM
> To: spug-list at pm.org
> Subject: SPUG: hashes, arrays, map, join
> 
> 
> 
> 
> 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/
> 
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      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/
> 
> 


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