in reply to Re: Re: Re: Re: Simple Encryption question
in thread Simple Encryption question

s/this/that/ s#this#that# s#this# "th" . "at" #e <-- the /e means (e)valuate the RHS before do +ing substitution # so the LHS of our RE is just this ^ begin string ( begin capture into $1 \d{16} 16 digits ) end capture into $1 # when we get a match of 16 digits at the begining of the string then +we have 2 events. # first $1 contains them and # second the RHS of the RE gets evaluated. $_ = 1; # set a var to $1 so we can modify it (can't do to $1 as +read only) tr/0-9/A-J/; # transliterate contents of $_ aka $1 $_; # perl will eval this with the net result that # our 'that' result is $_ which duly gets used # to replace our original digits

/e is quite handy at times. The reason we need the naked $_ at the end of the RHS is because when perl evaluates a function (like the RHS) the return value is the last thing Perl evaluated. Without the $_ this would be the return value from tr which is not the transliterated string itself but rather the integer count of the number of transliterations. So we put the $_ there. You could put "$_ hello" or anything else you liked and that is what would get subbed in.

cheers

tachyon

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Simple Encryption question
by rman (Initiate) on Dec 31, 2003 at 19:50 UTC
    Thanks for the patient explanation. I get it now. Also, A quick one hour reading of this doc http://www.perldoc.com/perl5.8.0/pod/perlretut.html helped me remember the fundamentals of regex cheers!.