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

I am trying to read a file and get the value from it. This file has only one value for ex: john Somehow it does not go into the loop to print and get the value. What am I doing wrong?
open(last_assignee, '<', "last_assignee.txt") or die "Unable to open + last_assignee.txt for READ\n"; # Get the last assigned value and store it to a variable while (<last_assignee>) { print $_; chop; if ($_ ne "") { $assignee_last = $_; } }

Replies are listed 'Best First'.
Re: reading a file
by CountZero (Bishop) on May 02, 2008 at 17:54 UTC
    Do not use chop is you wish to delete the EOL character(s). It will always cut off the last character, even if it is not a EOL, so you may loose a character.

    Much better to use chomp which only removes EOL character(s):

    As say the docs:

    This safer version of chop removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: reading a file
by toolic (Bishop) on May 02, 2008 at 16:15 UTC
    It seems to work for me. This code enters the while loop and prints out the value of the input file:
    > cat last_assignee.txt john > ./684200.pl In while loop john > cat 684200.pl #!/usr/bin/env perl #use warnings; #use strict; open( last_assignee, '<', "last_assignee.txt" ) or die "Unable to open + last_assignee.txt for READ\n"; # Get the last assigned value and store it to a variable while (<last_assignee>) { print "In while loop\n"; print $_; chop; if ( $_ ne "" ) { $assignee_last = $_; } }

    Are you sure your input file has anything in it?

    I intentionally commented out use warnings; use strict; just to try to duplicate your results. Make sure you use these in your code.

      yes I do have values. I think it is just a new line that was needed after the first line. I added a new line and it is working. I update this file in the script so I was just putting in the value and for some reason it did not like it.
Re: reading a file
by FunkyMonk (Bishop) on May 02, 2008 at 18:10 UTC
    If your file only contains one value, whay are you using a while to read it? Just use the diamond operator, <>:
    $_ = <last_assignee>; chomp; if ($_ ne "") { $assignee_last = $_; }

    As others have suggested, make sure you add strict and warnings, and use chomp rather than chop.

    Finally, a question: Your if and the assignment to $assignee_last looks to me like a red flag, but it's hard to tell without seeing what follows. What does your program go on to do if $_ eq ""?


    Unless I state otherwise, my code all runs with strict and warnings