in reply to How do I reverse the order of the first and last word of a string?

Pretty directly,

my $string = 'This is my program'; print do { my @arr = split ' ', $string; join ' ', @arr[-1, 1..$#arr-1, 0],$/ };
or,
my $string = 'This is my program'; { my @arr = split ' ',$string; print "@arr[-1, 1..$#arr-1, 0]$/" }
That code uses array slices.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How do I reverse the order of the first and last word of a string?
by GrandFather (Saint) on Mar 11, 2007 at 06:45 UTC

    and doing without the join:

    my $string = 'This is my program'; my @arr = split ' ', $string; print "@arr[-1, 1..$#arr-1, 0]";

    After all, we don't want to introduce too much on the first lesson.


    DWIM is Perl's answer to Gödel

      I think I added the similar code while you were writing that. In any case, join is probably easier to teach than $", the array interpolation seperator.

      After Compline,
      Zaxo

        .oO(Damn, now where did I put that tounge in cheek symbol?)


        DWIM is Perl's answer to Gödel