Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping with foreach 1

Status
Not open for further replies.

barazani

IS-IT--Management
Feb 22, 2001
19
0
0
US
hi to all,
i have a problem with foreach loops .
this is the loop
#!/usr/local/bin/perl
open(FILE,looptest);
@array=qw(one two three four);
foreach $i (@array){
print "$i \n";
while(<FILE>){
print &quot;$_\n&quot;;
}
}
my problem is that the while statemnt only gets executed once !
why is that ?
 
What are the contents of the file looptest? also, you may want to double check on whether or not it's being opened properly by saying:[tt]
open(FILE, &quot;looptest&quot;) or die &quot;Cannot open looptest: $!&quot;;[/tt]

with normal files that are properly opened, your while loop should work perfectly. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Hi ,
thanks for your reply
looptest has these lines in it
alpha
beta
gama
delta
if i use the while outside the loop it works !
barazani
the output of this script is asd folows
one
alpah
beta
gama
delta
two
three
four
i want it to be
one
alpah
beta
gama
delta
two
alpah
beta
gama
delta
and on ..... barazani
bara_zani@yahoo.com
&quot;Unix is user friendly. It's just selective about who its friends are.&quot;
 
oh, i see what you mean. sorry, i should have seen that the first time.
the <> counter will not automatically start over at the beginning of the file once it reaches the end. to do what you want, there are two ways(that i can think of at the moment). one is to read the file in to an array first thing, then close it. this is the easiest, and since it won't involve multiple redudant disk accesses, is probably the best.
the other would be to reset the <> counter to the beginning of the file and re-read it again. you do this with a 'seek' call in the following format:[tt]
seek(FILEHANDLE, 0, 0);[/tt]

the first zero is the offset (zero bytes), and the second is the whence, which can be any of the following:
0 = beginning of file
1 = current position
2 = end of file
doing this will work(put the seek line right before the 'while' loop inside the foreach loop), but like i said, is slow. the seek function is more useful for monitoring changing files or other datastreams.
you can also do this by closing and then opening the file again. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
thanks !
i put the file handle open at after the foreach ...
i am still new to perl so the seek function seemd a bit of an overhead right now .....
thanks again
and i liked the muskito->tent closing line .
good one :)
barazani barazani
bara_zani@yahoo.com
&quot;Unix is user friendly. It's just selective about who its friends are.&quot;
 
thanks.

and actually, the re-opening is almost exactly what the seek does, although you're right to avoid it.

good luck. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top