Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

ok, i have this bit of code, seems it could be done more elegantly with a single line -- anyone know how?

foreach (@ARGV) {
($a, $b) = split '=';
$args{$a} = $b;
}

Replies are listed 'Best First'.
Re: silly question
by chromatic (Archbishop) on Apr 27, 2000 at 19:23 UTC
    Mmm, map: %args = map { split /=/ } @ARGV; Nondestructive, too!
      Not entirely non-destructive. It's pretty damaging to %args. I'd suggest (although messy) if you wanted to keep the previous value of %args and merge in the new, you could do this:
      %args = (%args, map { split /=/ } @ARGV);
        I'm not sure whether this is better or worse. I suspect performance is better when %args is larger. If you're not interested in clobbering @_, use a temporary array. This won't win me any Perl golf either: map { @_ = split /=/; $args{$_[0]} = $_[1]; } @data;
Re: silly question
by snowcrash (Friar) on Apr 27, 2000 at 18:53 UTC
    the only way i come up with is using split twice...
    $args{(split/=/)[0]}= (split/=/)[1] foreach (@ARGV);
      hah, got another one: two splits less but @ARGV is destroyed :)
      s!(.+)=(.+)!$args{$1}=$2!e foreach (@ARGV);
Re: silly question
by zodiac (Beadle) on May 04, 2000 at 11:57 UTC
    map{ $args{$1}=$2 if /(.+)=(.+)/ }@ARGV; really nondestructive. plus it skips all values in @ARGV that do not match (that dont have text before and after the =)