in reply to File Manipulation

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.

Replies are listed 'Best First'.
Re: Re: File Manipulation
by davorg (Chancellor) on Sep 26, 2002 at 13:53 UTC

    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

Re: Re: File Manipulation
by helgi (Hermit) on Sep 26, 2002 at 13:47 UTC
    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

Re: Re: File Manipulation
by Helter (Chaplain) on Sep 26, 2002 at 13:56 UTC
    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!