in reply to japh with re

Nifty!

Not just for what it does and how it does it, but because it's probably the first obfuscated code I've been able to figure out on my own. However, some things were just assumptions or guesses.

So, in an attempt to help me really wrap my mind around this, build up my hubris, and learn something; I would appreciate it if (a|some) more knowledgable monk(s?) could (explain|verify) the following magic:

1) I think I figured this one out. At first I was confused about the 4 digit sequences. I was thinking the ones that started with a zero were octal, and was coming up with an unintelligible character sequence. But then I recalled something about the default match delimiters acting like double quotes. I read elsewhere on this site that a "number" like 0100 is octal, but "0100" is decimal. So apparently the captured data in the match effectively is double quoted, in this case insuring the numbers are decimal?

2) I seem to recall reading, but can't find it anywhere right now; that in an expression like

$1>=1000&&1000-$1||$1*1

it's value is the last thing evaluated. That expression can be re-written as
($1>=1000)     &&    (1000-$1)     ||     ($1*1)

so if $1>=1000, it has to evaluate 1000-$1, and can stop there since it's satisfied the ||, and the value of the expression is 1000-$1. If $1<1000 it won't bother with 1000-$1, and then evaluate $1*1, so the value of the expression is $1. At least, from my reading of the description of associativity, that's the order in which I presume it evaluates the terms. So, I take it, one of the ramifications of associativity is determining the ultimate value of a complex expression?

3) I've never seen a do{} with the while inside the braces, is this the same effect as having it immediately after them?

4) I've never seen a block before a for. Is this a similar idiom to reversing an if and letting you do this?

print $message if $verbose;

5) I copied the code into my text editor and put spaces in between each 4 digit sequence of the data while I was trying to figure it out. After I figured it out, I saved it and ran it, but it didn't work. I had to remove the spaces. The \G saves your place in the data, but it doesn't act like an anchor does it? I mean, if there's now a space before the next set of 4 digits, shouldn't it just skip the space to get to the digits? It didn't seem to.

Sorry to go on so long, but I've found this to be a site of great knowledge, and am hoping to learn much here.

TheEnigma

Replies are listed 'Best First'.
Re^2: japh with re
by Velaki (Chaplain) on Aug 19, 2004 at 22:01 UTC

    These explanations refer to the numbered items in the "above" post. Hope they help. :-)

    1. The four characters are treated as a string. Performing math on it converts it back to decimal, so "0100" * 1 = 100.

    2. I thought I'd exploit the short-cuts available in the logical operators to effect an if-then-else statement. "And" evaluates the right side only if the left side is true. "Or" evaluates the right side only if the left side is false. So,
      $a && $b || $c

      will evaluate pretty much to the same thing as

      if($a) { $b; } else { $c; }

    3. The do{} is a block of code executed for each element in the "for" statement modifier. I use it to contain the print...while statement. Inside the braces, the while is a modifer for the print statement. Outside the braces, it would modify the do{} block.

    4. Yes. See above.

    5. The \G saves the point of last match, so when you go to match again (continuing the global match) it picks up where it left off. It would be like chopping off everything before the point where \G is, and then trying to match /^(....)/. Now, I don't see any allowance for spaces in that, so, no, it won't skip the spaces.

      Hope this helped, -v
      "Perl. There is no substitute."