ellem has asked for the wisdom of the Perl Monks concerning the following question:

The idea is simple enough. Take a text file containing text like this:
Fred Flintstone Barney Rubble Dino The Dinosaur
remove all the spaces and then transliterate them as shown in the code below:
#!/usr/bin/perl use warnings ; use diagnostics ; use strict ; open (LIST, "sn.txt") or die "No file exists: $!" ; open (NEWFILE, '+> C:/Lou/Perl_Scripts/code/perl/pwd.txt') or die "Can +'t open file; $!" ; #I am intentionally erasing the file pwd.txt my($cname, $name, $pwd); while(<LIST>) { chomp; $cname = $name = $_ ; $name =~ s/\s//g; $name = $pwd = $_ ; $pwd =~ tr [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw +xyz] [2223334445556667778889990022233344455566677788899 +900] ; write NEWFILE ; } close LIST or die "Can't close file: $!" ; close NEWFILE or die "Can't close file: $!" ; format NEWFILE_TOP = ------ ------ NAME PWD ------ ------ . format NEWFILE = @<<<<< @<<<<< $name, $pwd .
My code is NOT removing the spaces. I have also tried $name =~ s/\+s//g;
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
Re: Removing Spaces From A File
by VSarkiss (Monsignor) on Mar 04, 2003 at 16:15 UTC

    Um, how do you know it's not? You clobber the value in $name right after you assign it:

    $name =~ s/\s//g; $name = $pwd = $_ ;
    Your code for removing blanks looks fine otherwise.

      Because it writes this
      ------ ------ NAME PWD ------ ------ Fred F 3733 3 Barney 227630 Dino T 3466 8
      I guess I am doing something else incorrectly. Thanks anyway.
      There's more than one way to do it, but only some of them actually work.

        The point is, after you remove the blanks in $name, you then assign a new value to it before doing the write. The output you're seeing is the result of that second assignment, not the result of the substition.

        As xmath points out below, you're copying around a lot of stuff, and it's not clear why. You may need to rethink part of your algorithm.

Re: Removing Spaces From A File
by xmath (Hermit) on Mar 04, 2003 at 16:24 UTC
    OK, I'll first X-part of the XY-problem.. you continuously copying values around between variables and then overwriting them again :-)
    $cname = $name = $_ ; # copies $_ into $name and $cname $name =~ s/\s//g; # removes the spaces from $name $name = $pwd = $_ ; # copies $_ into $name and $pwd $pwd =~ tr .... # transliterates $pwd

    The right code for what I guess you want would be:

    $name = $pwd = $_; # copy line into $name and $pwd $pwd =~ s/\s//g; # remove spaces from $pwd $pwd =~ tr .... # transliterate $pwd

    Now the Y-part.. what are you really trying to do? It look like you're generating passwords or serial numbers from the user's name. If so, then you should really look into using a hashing algorithm. There are plenty of modules available to do this, such as Digest::MD5 or Digest::SHA1.

    Update, since it's not trivial how to use MD5 to make a 6-digit number, here's an example:

    use strict; use warnings; use Digest::MD5 qw(md5); my $name = "Fred Flintstone"; my $key = substr unpack("I", md5 $name), -6; print "Name: $name\n"; print "Key: $key\n";

    Hope this helps

      OK I see. I wanted to have $name be spacesless, then generate the phone password. The transliteration is just a rough estimate of phone pads (ABC == 2, etc) that creates phone number passwords. So there isn't a huge need for security, thanks though.

      This solution will work.
      There's more than one way to do it, but only some of them actually work.
Re: Removing Spaces From A File
by Anonymous Monk on Mar 05, 2003 at 17:55 UTC
    And borrowing from the above code, TIMTOWTDI:
    #!/opt/perl5/bin/perl -w use strict; my($name,$pass); while(<STDIN>){ chomp; my $name=$pass=$_; $pass=~s/\s+//g; $pass=~tr[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz][2223 +3344455566 67778889990022233344455566677788899900]; print"$name\t\t$pass\n"; }
    You would call it from the command line like so:
    C:\perl trans.pl < sn.txt > pwd.txt