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

(This has been edited to make sense. I'ved added additional lines of code and a definition) I am attempting to read in a data file and then make variable assignments from the array. I don't want the '\n' at the end of each line. When I do this:
#!/usr/bin/perl -w open SSG, "<ADRX.ssg" || die "can\'t open ADRX.ssg"; @ssg = <SSG>; chomp @ssg; print $ssg[93];
I get bupkis. it means nothing in Yiddish
[ceidem@bizet ~/src/perl/ssg]$ ./pssg2.pl [ceidem@bizet ~/src/perl/ssg]$
If I remove the chomp(), the data are there in the array as it says on the box,
#!/usr/bin/perl -w open SSG, "<ADRX.ssg" || die "can\'t open ADRX.ssg"; @ssg = <SSG>; #chomp @ssg; print $ssg[93];
but then I've got the '\n'.
[ceidem@bizet ~/src/perl/ssg]$ ./pssg2.pl 2002 [ceidem@bizet ~/src/perl/ssg]$
If I use a foreach and a chomp, I get nothing. What gives? How can I remove the newline? Thank you in advance, - yam

Replies are listed 'Best First'.
Re: Reading a file into an array
by CountZero (Bishop) on Mar 11, 2004 at 23:02 UTC
    I tried your script and it worked the way it should (well with the addition of the comma in your open statement).

    Are you sure that the line-endings are as you expect them to be? They should be equal to the value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module) or otherwise chomp doesn't work.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Reading a file into an array
by Happy-the-monk (Canon) on Mar 11, 2004 at 22:58 UTC

    thnough I don't know the meaning of "bupkis", this works fine when you change
    open SSG "<asdf.ssg"
    to
    open SSG, "<asdf.ssg"

    If you meant to say you still got linebreaks, then there's a different problem.

    Sören

Re: Reading a file into an array
by matija (Priest) on Mar 11, 2004 at 22:55 UTC
    I don't know how you used foreach and chomp to get bupkis - (unless you had "bupkis" in the file ;-)

    You could do this:

    @ssg=<SSG>; chomp @ssg;
    You could also use File::Slurp, and chomp the return from that...
Re: Reading a file into an array
by Anonymous Monk on Mar 12, 2004 at 03:59 UTC

    Although entirely unrelated to your posted problem, I'd like to point out that you aren't actually checking the return value of your open call like you may think you are. The '||' operator has higher precedence than the comma so:

    open SSG, "<ADRX.ssg" || die "can\'t open ADRX.ssg"; # is the equivalent of saying: open(SSG, ("<ADRX.ssg" || die "can\'t open ADRX.ssg"));

    Either use the lower precedence or operator, or use parentheses appropriately:

    open(SSG, "<ADRX.ssg") || die "can\'t open ADRX.ssg"; open SSG, "<ADRX.ssg" or die "can\'t open ADRX.ssg";
Re: Reading a file into an array
by yam (Novice) on Mar 12, 2004 at 00:57 UTC
    Gah! DOS file type on my Mac. Rotten little '\r'. That was it. Sorry for being dumb... - yam