in reply to Re^2: 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?

Welcome to the Monastery. Generally you will find that you get the most help when you show that you have done some work and demonstrate that you are not just looking for someone else to do your homework for you.

I suspect that if you hand in the code I gave above as a homework answer your teacher will either not believe that you wrote it, or will think that you are a smart arse and shouldn't be in the course.

However if you read the documentation for split and join you may find that you can come up with a more acceptable solution for yourself.


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

Replies are listed 'Best First'.
Re^4: How do I reverse the order of the first and last word of a string?
by jeanluca (Deacon) on Mar 10, 2007 at 22:42 UTC
    Thats an impressive example GrandFather! do you also have some links/docs to help me understand your example a little bit better ?

    Thnx
    LuCa

      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
        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>

        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