in reply to (Ovid) Re: Is a global variable better than repetition within variables?
in thread Is a global variable better than repetition within variables?

As of perl5.6 values() returns the actual variables and not copies. This means that you can do fancy stuff like     $_ = $prefix . $_ foreach values %hash; Actually, I'd even prefer     $hash{$_} = $prefix . $hash{$_} foreach keys %hash; over the map() approach, since as you say, there can be hundreds of them (or 18278 as in your example! ;)) and building up another list of that would be a waste. Or is there any funky optimization going on that I'm unaware of?

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: Is a global variable better than repetition within variables?
by blakem (Monsignor) on Jan 27, 2002 at 12:44 UTC
    Just a bit of weekend golf/obfu....
    s//$prefix/for values%hash;

    -Blake

      I was about to suggest that too, just for fun. But I chose not to. Didn't want to start a golf/obfu thread. :)

      Anyhow, there's a bug in your code. You need to have something as pattern. Otherwise the empty pattern magic will kick in.

      -Anomo
        What bug are you talking about? We want the empty pattern "magic" to kick in... at least the magic where it matches the emptyness sitting infront of a string. Thats how we add the prefix to each value in the hash...
        #!/usr/bin/perl -wT use strict; my $prefix = 'dog'; my %hash = map {++$;,$_} (qw(ged house ma matic),''); print "Before:\n"; print "$_ => '$hash{$_}'\n" for keys %hash; s//$prefix/for values%hash; # Add the Prefix... print "\nAfter:\n"; print "$_ => '$hash{$_}'\n" for keys %hash; __END__ Before: 1 => 'ged' 2 => 'house' 3 => 'ma' 4 => 'matic' 5 => '' After: 1 => 'dogged' 2 => 'doghouse' 3 => 'dogma' 4 => 'dogmatic' 5 => 'dog'

        -Blake