#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1

    Can't understand a error I'm having


    Say I had a text file with a line(s) like this :

    g 5 h 6 f 9 g 3

    My program is supposed to update it to something like this:

    g 8 h 6 f 9 .

    Check out my script thus far

    Code:
    #!/usr/bin/env perl
    use warnings;
    use strict;
    
    my @count_hash;   #define my array
    print "Enter the name of your file\n";
    my $FILENAME = <STDIN>;
    
    open (my $FILE, "<", "$FILENAME")
                             or die "ERROR";
    while (my $line = <$FILE>){
            chomp $line;
            my($letter, $number) = split / /, $line;
            @count_hash = ["$letter"," $number"];
         }
    print "@count_hash\n";
    The program starts running, but than I get an error message on line 9 about newline characters. I don't know how to make any sense of it.

    And I know that my current program isn't going to do what I want it to do yet, this is just to get the skeleton of the program set up.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    You need to chomp $FILENAME before using it in the open call.

    The die statement should include the filename and reason it failed.

    You should not quote single vars. See: What's wrong with always quoting "$vars"?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1
    Originally Posted by FishMonger
    You need to chomp $FILENAME before using it in the open call.

    The die statement should include the filename and reason it failed.

    You should not quote single vars. See: What's wrong with always quoting "$vars"?
    Why do you have to chomp it?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    User input received via STDIN will include the newline terminator and filenames don't include the newline terminator so, that line terminator needs to be removed otherwise the open call will fail.

    "file.txt" is not the same as "file.txt\n".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1
    Thanks fish, that cleared up the acne.

IMN logo majestic logo threadwatch logo seochat tools logo