in reply to Re^4: How do I reverse the order of the first and last word of a string?
in thread How do I reverse the order of the first and last word of a string?

The split and join I'm sure you understand. I suspect the bit you find interesting is the "swap without temporary" - an old assembly language trick. In situations where there weren't enough registers to use one for temporary storage when swapping two registers and the time cost of going off chip was great this trick could be used to peform the exchange. The three xors sort of slide the bits from each regiser through each other. Consider (note binary values - this is not Perl):

a = 0011 b = 1010 a = a ^ b (a now contains 1001) b = a ^ b (b now contains 0011 - a's original contents) a = a ^ b (a now contains 1010 - b's original contents)

In the example code two strings get swapped using the technique. However if the strings are different lengths then the shorter string will end up with trailing null characters. The substitution strips the nulls. A tr could have been used instead, but substitutions are more often used in general so a better thing for the OP to be made aware of.


DWIM is Perl's answer to Gödel
  • Comment on Re^5: How do I reverse the order of the first and last word of a string?
  • Download Code

Replies are listed 'Best First'.
Re^6: How do I reverse the order of the first and last word of a string?
by blazar (Canon) on Mar 11, 2007 at 11:16 UTC
    I suspect the bit you find interesting is the "swap without temporary" - an old assembly language trick.

    Hehe, actually I'm very fond of this technique because I... "invented it!" Well, to be fair it was during my first exposure to some programming language, in high school: it was an experimental course and the language was Turbo Pascal. (Not that I remember any significative Pascal any more, I was 13 back then, I guess.) The teacher asked us to write some code to swap two variables as an exercise and in turn I asked whether we should do so with a third variable, because to me it seemed so obvious that it was not worth asking at all. She answered: "do whatever you want". So I "decided" that the requirement couldn't be the ridiculously simple solution with a temporary variable and worked my mind around one that wouldn't use it. The variables were integers and my solution was (in pseudo code):

    a = a - b b = a - b (a-2b) a = a - b (a-b-a+2b=b) b = b + 2a (a-2b+2b=a)

    (In parentheses the full expressions in terms of the initial values of a and b.)

    This is actually the same trick as the XOR one, since it only relies on ring properties and XOR is both a minus and a plus in Z_2. Of course in that case the last step is not required because in the field Z_2, 2x=0 for all x.

    Needless to say, the teacher did expect the temporary variable trick and pointed out that mine, despite how proud of it I were, would only work with numerical data, which is true. All in all she wasn't very happy. Nor was I: if only she had confirmed that she wanted a third variable in the first place...

    </anecdote>

      I would never thought about that, I guess I'm a lazy programmer, I used to use a third variable but now I use
      ($a, $b) = ($b, $a) ;

      Anyway, your code could be reduced by one line
      b = b + a ; a = b - a ; b = b - a ;
      But I guess you already knew that!

      LuCa
        Anyway, your code could be reduced by one line
        b = b + a ; a = b - a ; b = b - a ;

        Actually, I absolutely can't remember what my "original" code was really like. I "reconstructed" it this morning but didn't put much thought in it: just wanted to stick with with a scheme like that of the XOR trick, with the same operation on all (first three) lines. Of course, since XOR is again both plus and minus in Z_2, then the above code is equivalent to it too.

Re^6: How do I reverse the order of the first and last word of a string?
by jeanluca (Deacon) on Mar 11, 2007 at 16:24 UTC
    Thats all very clear now, thnx

    I have an other (a little bit off topic) question, its about
    map {s/[^a-z ]//gi} @words;
    I do understand the map part, but I don't really understand what it is supposed to remove ?

    I've tested it a little bit more in detail like
    my $x = " aabbaaccaa " ; $x =~ s/[^a-z ]//g ;
    What exactly is the role of the ^, because I think thats the point where I loose it!!

    Thnx
    LuCa
      What exactly is the role of the ^

      In a character class (the square brackets thing), if you put ^ (called a "caret") first before any characters, it negates the whole class, so it means "any character _except_ one of these". (You can include a caret in a character class and have it just be a literal ^ character, if it's not the first thing; it only has this special negate-the-class meaning if it's first.)

      This is all only in a character class, though. In other regular-expression contexts, the caret is a zero-width positive assertion that matches only at the beginning. (Whether it matches at the beginning of the line or the beginning of the string depends on the options you're using.)

      So this regular expression matches any string that starts with a vowel:

      /^[aeiou]/i

      But this one matches any string that starts with something *other* than a vowel:

      /^[^aeiou]/i

      (Technically, I should say "unaccented Latin-alphabet vowel" in both of those statements, but if you assume strings are composed of printed ASCII characters then what I said is good enough. The extra verbiage would only be necessary to accommodate other character sets, notably Unicode. And anyway, the meaning of the word "vowel" is really not the main point here.)

      -- 
      We're working on a six-year set of freely redistributable Vacation Bible School materials.
        I already thought something strange was going on with the caret. Thanks!

        LuCa