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

Thank you perlmonks for the help. substr works fine in my case. yeah, this is by no means a robust encryption. One more question: How do i skip the first and last lines of the file from this substitution? The first and last lines are headers and footers of the file and needs to be preserved. thanks. I am revisiting perl after a long time and am rusty. So, appreciate all your help.

Replies are listed 'Best First'.
Re: Re: Re: Simple Encryption question
by Roger (Parson) on Dec 30, 2003 at 00:05 UTC
    It depends on how your header and trailer data look like. If they do not start with numerics, then it's easy...
    while (<$filehandle>) { tr/blah/blah/ if /^\d{16}/; # do the translation print; # output result }
    If your header and trailer look similar to the rest of the data, and if your file is small, you could read everything in to memory first...
    my @data = <$filehandle>; print $data[0]; # print first line for (1..$#data-1) { # skip first and last line $data[$_] =~ tr/blah/blah; print $data[$_]; } print $data[-1]; # print last line
    Otherwise it will get a bit messy.

Re: Re: Re: Simple Encryption question
by tachyon (Chancellor) on Dec 30, 2003 at 00:07 UTC

    If your file is small, just read it into an array first:

    open F, $file or die $!; my @data = <F>; for my $i ( 0..$#data ) { if ( $i == 0 or $i == $#data ) { #first or last line so do whatever print $data[$i]; } else { # not first or last line $data[$i] =~ s#^(\d{16})#$_ = $1; tr/0-9/A-J/; $_#e print $data[$i]; } }

    cheers

    tachyon

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