in reply to Simple Encryption question

($_ contains a line of the file)     substr( $_, 0, 16) =~ tr/0-9/AB\-C-HJK/; That will make the substitution directly in $_ . A substitution like this is not sturdy encryption, just an obscuration.

Update: Changed tr string to fit the spec.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Simple Encryption question
by rman (Initiate) on Dec 29, 2003 at 21:00 UTC
    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.
      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.

      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