in reply to merging files using map

#!/usr/bin/perl -w @t = qw( aaaa bbbb ccccccc dddddd ); @n = map {$i++." ".$_} @t; print map {$_."\n"} @n; __END__ 0 aaaa 1 bbbb 2 ccccccc 3 dddddd

Replies are listed 'Best First'.
Re^2: merging files using map
by keszler (Priest) on Jul 01, 2004 at 11:25 UTC
Re^2: merging files using map
by epistrophy (Initiate) on Jul 01, 2004 at 11:55 UTC
    This works fine, thank you. But it is not exactly what I want, I guess my question wasn't clear enough. In the first list I want to start each line with a number divisble by 5 (x % 5 == 0), the second list should have numbers prepended that are odd, except for odd numbers that are divisble by 5. And different restrictions apply to the numbers of the third and the fourth list. In a test of the algorithm I used, I did this:
    @a = (0..100); @b = map {if ($_ % 2 != 0 && $_ % 5 == 0){$_}} @a; print @b;
    This works fine as far as producing the numbers I want concerns. The problem is that in the above example I use the $_. In the real script I cannot do that since the $_ has the present value of the list I am processing. best, ruben
      Divisible by 5:
      #!/usr/bin/perl -w @t = qw( aaaa bbbb ccccccc dddddd ); # assumes $i is undef or == 0 @n = map {$i+=5." ".$_} @t; print map {$_."\n"} @n;
      Odd, starting at 1, but not divisible by 5:
      #!/usr/bin/perl -w @t = qw( aaaa bbbb ccccccc dddddd ); $i = -1; # the 2nd semicolon is not required, but adds clarity @n = map {$i+=2; if ($i%5==0){$i+=2}; $i." ".$_} @t; print map {$_."\n"} @n;
      Your examples seem to indicate that you misunderstand the use of map. For example, your
      @newwords = map {$l % 5 == 0; $l," ",$_} @words;
      There's a test of $l, but nothing happens no matter the result. The $l," ",$_ might make sense as an argument to the print command, but not here. If it were $l." ".$_ then @newwords would be filled with items consisting of the value of $l followed by a space followed by an item from @words. However, $l would not be changed so every @newwords item would have the same number.