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

Ok, I wrote a little script which takes a line from a txt file and copy it to my html site. Than it delete it. However it work's however it adds additional parameters. It just goes to the other line. I need it to stay perfectly the same. for any help I would be very heppy Dan
$random_file = "/home/sites/site70/web/cgi-bin/i-merchant/admin/setup/ +ric.txt"; open (FILE, $random_file); @LINES=<FILE>; close(FILE); srand; $OTP = "$LINES[(int rand@LINES)]"; $PWFILE = $OTP; open (FILENEW, ">$random_file"); foreach $LINES (@LINES) { if ($PWFILE eq $LINES){ $LINES = "" } else { print FILENEW $LINES; } } close (FILENEW); $PPP = $OTP;

Replies are listed 'Best First'.
Re: Wipking
by rdfield (Priest) on Sep 26, 2002 at 13:25 UTC
    First up, try adding:
    use strict; use warnings;

    I think you'll be surprised at what falls out.

    rdfield

    Update: wrt jmcnamara's following comment: as the node was originally laid out there seemed to be confusion between @ and $ ... davorg's reformat removed this confusion.


      I think you'll be surprised at what falls out.

      What falls out?

      There aren't any errors here for warnings or strict to pick up.

      While it is generally a good practice to use these strictures, they won't help the OP in this case.

      --
      John.

Re: Wipking
by Helter (Chaplain) on Sep 26, 2002 at 13:31 UTC
    I'm assuming this is a snippet from your actual script.
    You never tell us what $OTP, $PPP, $PWFILE are.
    Your description of the problem is a little hard to understand.
    You want to run this script to take a line from a text file, and copy it to the html site.
    Then you want to delete the file, or just the line in the file?
    It adds additional parameters? To what? Where? The line that gets to the html page? To the original file?
    You need it to stay perfectly the same, the original file, the line you copy or something else?
Re: File Manipulation
by wipking (Initiate) on Sep 26, 2002 at 13:39 UTC
    I try to write it different. I have to get a OTP (one time password) from a txt file. The script has to get one of the passwords and than delete it. The password is used in a transection string. The script does exactly that, however it adds parameters on it which are not part of the original password. To be exact it goes to a second line.

      You need chomp to remove the newline from your password.

      my $random_file = "/home/sites/site70/web/cgi-bin/i-merchant/admin/set +up/ric.txt"; open (FILE, $random_file) or die $!; my @LINES=<FILE>; close(FILE); my $OTP = splice @LINES, rand @LINES, 1; chomp $OTP; open (FILENEW, ">$random_file") or die $!; print FILENEW, @LINES; close (FILENEW); my $PPP = $OTP;

      Also, if more than one process can access the file at the same time then you need to look at file locking.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      This is a FAQ. Check out:

        perldoc -q change line file

      A.K.A. "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?"

      -- Regards,
      Helgi Briem
      helgi AT decode DOT is

      So you want to remove a random element, here's the code that works for me.
      $random_file = "test.txt"; open (FILE, $random_file); @LINES=<FILE>; close(FILE); srand; # Choose a random line #scalar(@LINES) is the number of elements in the array $OTP = "$LINES[(int rand(scalar(@LINES)))]"; $PWFILE = $OTP; open (FILENEW, ">$random_file"); foreach $LINES (@LINES) { if ($PWFILE eq $LINES){ $LINES = "" } else { print FILENEW $LINES; } } close (FILENEW); # If you want to remove the newline, I don't know much # about html stuff...it may cause problems? #chomp( $OTP ); $PPP = $OTP;
      I tested it with a file that looks like this:
      test test1 test2 test3 test4
      It removed a random element from the file, and had it in $PWFILE. I don't know about the rest of the file, but you could probably remove $PPP and $PWFILE and just use $OTP.

      I also did not fix the style, I would avoid using all capital var. names except for filehandles.

      Hope this helps!