in reply to Passing Arrays to Subroutines
You're not paying attention to what you've asked perl to do. You say:If I replace the 2 with 0 I get what looks like the first line of the data file printed but I expected to get just the first item
Yet, looking at your code, you read all of the lines of your file into an array, then return that array from your readdata() subroutine. In your main bit of code, you print out the 3rd item in that array of lines. So, of course you're going to get an entire line of data!
What you apparently want to do is subsequently split each line on whitespace (maybe?) and then print out the bits of the line that interest you.
... my @lines = readdata(); foreach my $line (@lines) { my @fields = split ' ', $line; print $fields[2]; } ...
It would greatly help your cognitive processes if you chose better names for your subroutines and variables; "data" is just too generic. Use the variable names to describe what the data are.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Passing Arrays to Subroutines
by shemyaza (Novice) on Nov 26, 2003 at 16:46 UTC | |
by jeffa (Bishop) on Nov 26, 2003 at 18:40 UTC |