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

Hi Monks, I want to merge a couple of files and sort the output. I have stored the files to be merged in separate lists and I want to add a number to each element of each list with map. The numbers are to be used later by 'sort' to put the lines in a particular order. Perhaps an example...
@newsent = map {if ($i == 0){$i + 1} elsif ($i % 10 == 0){$i += 10 +}; $i," ",$_} @testsent; @newfillers = map {$j % 2 == 0 && $j % 10 != 0;$j," ", $_} @fillers +; @newfoils = map {unless ($k == 1){$k % 2 != 0 && $k % 5 != 0}; $k, +" ",$_} @foils; @newwords = map {$l % 5 == 0; $l," ",$_} @words;
Unfortunately, no number was put in the new list. I thought that I had to initialize a value for $i to $l, but when I do that the part of map that says which numbers should be added seems to be ignored. Here's my question(s): Do I have to initialize a value, and if so, where? Is map the best solution to my problem. Should I use a hash, and if so, why and how? best, Ruben van de Vijver

Replies are listed 'Best First'.
Re: merging files using map
by keszler (Priest) on Jul 01, 2004 at 11:09 UTC
    #!/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
      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.