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

Using the __DATA__ how do I get this to print in reverse. I have tried puttin the reverse function in the print statement but no luck.
while (<DATA>) { $_ =~ s/oldpage/newpage/g; print; } exit 0; close(DATA); __DATA__ title oldpage oldpage htm tab aaaaa
I would like it to print in reverse order such as:
aaaaa tab newpage htm newpage title

Replies are listed 'Best First'.
(jeffa) Re: Reverse Function
by jeffa (Bishop) on Jun 29, 2002 at 00:52 UTC
    How about an array?
    my @array = map { s/oldpage/newpage/g; $_ } <DATA>; print reverse @array; __DATA__ title oldpage oldpage htm tab aaaaa

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      jeffa's answered the question, but , to indulge in a little golf, it can be done in one line :
      print reverse map {s/oldpage/newpage/g; $_} <DATA>;

        Well, if you're gonna golf... ;^)

        s/oldpage/newpage/g, print for reverse <DATA>; # 46 s/oldpage/newpage/g,print for reverse<DATA> # 43

            --k.


        A reply falls below the community's threshold of quality. You may see it by logging in.
      what jeffa said cept I'd save some typing and chain it.
      print reverse map { s/oldpage/newpage/g; $_ } <DATA>; __DATA__ title oldpage oldpage htm tab aaaaa