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

Brilliant! This works fine.

I am curious to know how this statement works:

$data[$i] =~ s#^(\d{16})#$_ = $1; tr/0-9/A-J/; $_#e

I understand some pieces of it:
-> the s is for substitution; ^d16 is for first 16digits;
-> tr does the conversion. # delimits;

Can you please let me know, how the whole piece fits in to do the substitution.

thanks

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Simple Encryption question
by tachyon (Chancellor) on Dec 30, 2003 at 22:10 UTC
    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

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