http://www.perlmonks.org?node_id=472186


in reply to Pattern matching

open OUTPUT, join "", map chr, ,,,,,,,,,112+($\=\1)-$\,,,,,,, ,97+($/=\1)-$/,,,,,,,,114+($\= \1)-$\,,,,,,116+($/=\1)-$/,,,, ,51+($\=\1)-$\,,,,,46+($/=\1)- $/,,,116+($\=\1)-$\,,,120+($/= \1)-$/,,,,,,,,,,,,116+($\=""); while(<OUTPUT>){{$\="$\$_";$_= $\;$\ = substr((push @w,$\),0, 0)if$\=~/\s$/msg;}}print grep{ eval'/^\b.*(p).*$/msgi'}@w


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Pattern matching
by xorl (Deacon) on Jul 06, 2005 at 12:52 UTC
    Instead of wasting time lecturing the guy about not being a homework site, holli's code should have the first responce. The OP won't understand it (heck even I don't) and the teacher will know the guy cheated. He'll either fail the assignment or figure out what the code does. Whichever one happens, he'll learn the lesson he needs to be taught.
      The OP won't understand it (heck even I don't)
      Want a spoiler?
      +($\=\1)-$\ evaluates to zero, so we can take those out.
      +($/=\1)-$/ evaluates to zero too, just we need one of them to set the input separator $/ to 1 (bytewise reading).
      We can also take out the subsequent commas from the join (since they do nothing), so the first line becomes:
      open OUTPUT, join "", map chr,112,114,116,51,46,116,120,116; $\=""; $/=\1;
      The numbers are the ordinals of the single chars of the filename "part3.txt".

      Now for the while loop. If we write it clearer it looks like
      while(<OUTPUT>) { $\="$\$_"; $_=$\; $\ = substr((push @w,$\),0,0) if $\=~ /\s$/msg; }
      $\="$\$_"; simply concatenates the read char with $\.
      $_=$\; has no effect.
      $\ = substr((push @w,$\),0,0) if $\=~ /\s$/msg; pushes $\ to an array if the last char of $\ is a whitespace or newline, thus a "word".
      In the same statement $\ is cleared because the return value of substr(something, 0,0) is empty.

      The last part is a simple grep of the array we built. We can safely leave out the eval so it becomes:
      print grep{ /^\b.*(p).*$/msgi } @w
      Alter the regex here to your liking.


      holli, /regexed monk/