Re: reading file into an array
by lostjimmy (Chaplain) on Dec 05, 2008 at 20:53 UTC
|
I think we're going to need to see more code. There is no way that you could be getting a multidimensional (read: array of arrays) array from reading a file. I'd like to see what you are doing
An easy fix to start on line 14 would be something like:
#!/usr/bin/perl
use strict;
use warnings;
for (1..13) {
<DATA>;
}
while (<DATA>) {
print;
}
__DATA__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [reply] [d/l] |
|
When i cut the file into about 500 lines it worked fine, but with my test file of 2800 lines it put out the wrong output.
I have it running. I used:
while(<$file>){
...
}
and just used an arbitrary line counter to have it start processing the code once it hit line 14 and above.
I just tested similar code but just had it read the file into the array and then print it out to a text file and checked the text file and it was all there so what I was seeing in eclipse was misleading (I think it breaks it into 1000 line chunks to make it easier to read through manually). And I totally just had an 'aha' moment. I was parsing the date out of each line to test it against another file I had to make sure the lines I was comparing are the same date and time. Initially I was only taking the date from line 14 because I at first didn't realize one file would have multiple dates. So when I changed it to parse the date from every line I forgot to change the date reading from
$fileContents14 to $fileContents$x
So it wasn't looking like it was going through all the info because the test of making sure the same date is being tested failed for the whole document. And when I tested with only 500 lines the date changed to the correct date within the first 14 lines.
I don't know if much of that made sense without knowing what the program does and stuff but I now feel stupid for posting this question. I seriously played with this for hours before posting and couldn't figure it out, though.
| [reply] |
|
| [reply] |
Re: reading file into an array
by McDarren (Abbot) on Dec 05, 2008 at 21:03 UTC
|
for(my $x=14; $x<@fileContents; $x++)
Array indexes in Perl begin with zero, so that's actually going to start at line 15.
Also, a more perlish way of initiating that loop would be:
for (13 .. $#fileContents) {
print "$fileContents[$_]\n"; # Or whatever
}
However, a much better way to process a file line by line is with a while loop. Something like this:
my $lines_to_skip = 14;
while (my $line = <$file>) {
# $. will give you the current line number of the file
next if ($. <= $lines_to_skip);
chomp($line);
# Do stuff with $line
}
Hope this helps,
Darren :)
| [reply] [d/l] [select] |
|
my $linesToSkip = 14;
my $discard = <$file> for 1 .. $linesToSkip;
while( my $line = <$file> )
{
chomp $line;
...
}
Calling the scalar $discard makes it clear that it is something we are not interested in whereas $line might be something we want to work with.
I hope you find these thoughts of interest.
Cheers, JohnGG | [reply] [d/l] [select] |
|
I can't help mentioning, because I was so happy when I stumbled across it, the following cute syntax for skipping a constant number of lines using the range operator:
while ( my $line = <$file> ) {
next if 1 .. 14;
do_stuff;
}
(Unfortunately, it doesn't work with a variable—for example, next if 1 .. $lines_to_skip—since the range operator just tests whether $lines_to_skip is a true value, not whether it's equal to $..) | [reply] [d/l] [select] |
|
Sorry, it starts on line 15. Thanks for the reply. It is working now. And I like your way of the while loop much better than what I used. However, I'm new to perl and don't fully understand your for loop. I assume the "13 .." just means start at 14 but why is there a '#' in front of the fileContents and why $ instead of @? Java was my first language so perl is very different and its taking a lot of getting used to.
Thanks for the replies!
| [reply] |
|
my @foo = qw/a b c/;
print "$foo[1]\n"; # Prints b
print "@foo\n"; # Prints a b c
print "$foo[$#foo]\n"; # Prints c (but you would probably never do th
+is)
print "$foo[-1]\n"; # Also prints c (a better way than the previou
+s)
print "@foo[0,2]\n"; # Prints a c
Cheers,
Darren :) | [reply] [d/l] |
|
| [reply] [d/l] [select] |
Re: reading file into an array
by SilasTheMonk (Chaplain) on Dec 05, 2008 at 20:54 UTC
|
vassago Please do two things:
- Give us a link to a wep-page explaining what "epic" is.
- Post the code here. Be careful to post the code inside "code" tags as explained in the stuff below the text entry box.
| [reply] |
|
| [reply] |