in reply to Transposition of each letter pair in string

$\ = "\n"; my $foo = 'thisisatest'; print for map { substr((my $bar = $foo), $_, 2, reverse substr $foo, $_, 2); $bar; } 0 .. -2 + length $foo;

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re^2: Transposition of each letter pair in string
by Aristotle (Chancellor) on Dec 29, 2002 at 16:15 UTC
    ++, but why the complicated map games? :)
    my $foo = 'thisisatest'; for(0 .. -2 + length $foo) { substr((my $bar = $foo), $_, 2, reverse substr $foo, $_, 2); print $bar, $/; }

    Makeshifts last the longest.

      ++, but why the complicated map games? :)

      I had it without "for" first. print takes a list, map creates one. It made sense. But it all was printed on one line, so I added -l and for to make it print every item on its own line.

      When it functioned correctly, I pasted it in my web browser, and formatted it. Whenever you see me write $\ = "\n"; here, it's safe to assume I used only the command line to code and test, and used -l there.

      If I were to rewrite it, I'd probably have map generate strings that include \n as follows:

      my $foo = 'thisisatest'; print map { substr((my $bar = $foo), $_, 2, reverse substr $foo, $_, 2); "$bar\n"; } 0 .. -2 + length $foo;

      map feels natural to me. I use it a lot, and love it.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        Ok, that's logically the other WTDI. The for map combination just didn't make sense in this case though I too have used it as a lazy quick solution for -l oneliners. Perl++ :)

        Makeshifts last the longest.