Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

RE: Re: Unexpected Array

by ImpalaSS (Monk)
on Nov 13, 2000 at 23:33 UTC ( #41390=note: print w/replies, xml ) Need Help??

in reply to Re: Unexpected Array
in thread Unexpected Array

here is the code
$file = "/usr/local/rf-north/cgi-bin/chandata.txt"; open (FH, $file) || die "Unable to open file $file\n"; @data = <FH>; close (FH); foreach $item (@data){ @line = split (/ /, $item); push(@finalarray, @line); }

Does this help?>
Thanks Again

Replies are listed 'Best First'.
RE: RE: Re: Unexpected Array
by Fastolfe (Vicar) on Nov 13, 2000 at 23:35 UTC
    It would help if we could see the contents of a sample line. I suspect it looks like this:
    some stuff some more stuff
    with a bunch of spaces. You are splitting on / /, which is a single space, which means you are going to get a lot of empty elements in your resulting list. You probably want to change your split statement to split on /\s+/, which will catch all whitespace between elements.
RE: RE: Re: Unexpected Array
by autark (Friar) on Nov 13, 2000 at 23:38 UTC
    It seems to me that you want to create an array containing the words of the file. If you split on space like you have done split / /, $item, then you will only split on one space. I think you want to split on:
    split /\s+/, $item
    or just:
    split ' ', $item
    The last one will strip all blanks at the start as well as on the end of the string.


RE: RE: Re: Unexpected Array
by swiftone (Curate) on Nov 13, 2000 at 23:35 UTC
    Fastolfe is in all likelyhood completely correct.

    Don't forget that $item will have a newline at the end of it. That's not the problem here, but it may not be what you intend.

    Also, if chandata.txt gets very large, you're holding it in memory twice. You may want to loop through the file line by line and just extract the info you want.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://41390]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-09-22 09:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found