Thats an impressive example GrandFather! do you also have some links/docs to help me understand your example a little bit better ?
Thnx LuCa
| [reply] |
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
| [reply] [d/l] |
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> | [reply] [d/l] [select] |
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 likemy $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 | [reply] [d/l] [select] |