Click to See Complete Forum and Search --> : For Loop


dntel
02-15-2011, 08:59 AM
Hey Guys,

@cellNumbers = ('111', '222', '333');
foreach (@cellNumbers) {
count +1 $_;
}


Is that the correct loop to use for running through a file and for each of 111 or 222 to add a value and leep counting until it reaches the end?

Sixtease
02-16-2011, 01:09 AM
Well, no. This doesn't look right (count +1 $_). But I don't understand what you're trying to do.

running through a file ... until it reaches the end
For that, you'd need to open a file and loop over its lines (where you can adjust the notion of what is a line):
open my $file_handle, '<', 'filename.txt' or die "Couldn't open filename.txt: $!";
while (<$file_handle>) {
my $line = $_;
# do something with the $line
}

for each of 111 or 222 to add a value and leep counting
I don't get this part. What do you mean?