Filehandle within foreach loop

--001a114e5bfc49a02f055423b645
Content-Type: text/plain; charset="UTF-8"

Hello All,

I would like to read multiple files and process them.But we could read the
first file alone and the rest are skipped from the while loop. Please
correct me where am i missing the logic.Thanks.

use strict;
use warnings;
my @files=qw(Alpha.txt Beta.txt Gama.txt);

foreach my $file (@files)
{
open (my $FH, $file) or die "could not open file\n";

while( my $line = <$FH> ){
#[process the lines & hash construction.]
}
close $FH;
}

Regards,
Kamal.

--001a114e5bfc49a02f055423b645
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello All,<div><br></div><div>I would like to read multipl=
e files and process them.But we could read the first file alone and the res=
t are skipped from the while loop. Please correct me where am i missing the=
 logic.Thanks.=C2=A0</div><div><br></div><div><div>use strict;</div><div>us=
e warnings;</div><div>my @files=3Dqw(Alpha.txt Beta.txt Gama.txt);</div><di=
v><br></div><div>foreach my $file (@files)</div><div>{</div><div>open (my $=
FH, $file) or die &quot;could not open file\n&quot;;</div><div><br></div><d=
iv>while( my $line =3D &lt;$FH&gt; ){</div><div>#[process the lines &amp; h=
ash construction.]</div><div>}</div><div>close $FH;</div><div>}</div></div>=
<div><br></div><div>Regards,</div><div>Kamal.</div></div>

--001a114e5bfc49a02f055423b645--
0
kamal
7/12/2017 7:20:42 PM
perl.beginners 29239 articles. 3 followers. Follow

6 Replies
11 Views

Similar Articles

[PageSpeed] 24

--94eb2c0c9bf0cdda9005542426d8
Content-Type: text/plain; charset="UTF-8"

That code will read each line from each file.  The problem is likely in the
part that says:

#[process the lines & hash construction.]

What are you doing there?

On Wed, Jul 12, 2017 at 3:23 PM perl kamal <kamal.perl@gmail.com> wrote:

> Hello All,
>
> I would like to read multiple files and process them.But we could read the
> first file alone and the rest are skipped from the while loop. Please
> correct me where am i missing the logic.Thanks.
>
> use strict;
> use warnings;
> my @files=qw(Alpha.txt Beta.txt Gama.txt);
>
> foreach my $file (@files)
> {
> open (my $FH, $file) or die "could not open file\n";
>
> while( my $line = <$FH> ){
> #[process the lines & hash construction.]
> }
> close $FH;
> }
>
> Regards,
> Kamal.
>

--94eb2c0c9bf0cdda9005542426d8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">That code will read each line from each file.=C2=A0 The pr=
oblem is likely in the part that says:<div><br></div><div><span style=3D"co=
lor:rgb(33,33,33)">#[process the lines &amp; hash construction.]</span><br>=
</div><div><span style=3D"color:rgb(33,33,33)"><br></span></div><div><span =
style=3D"color:rgb(33,33,33)">What are you doing there?</span></div></div><=
br><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed, Jul 12, 2017 at 3:23=
 PM perl kamal &lt;<a href=3D"mailto:kamal.perl@gmail.com">kamal.perl@gmail=
..com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr=
">Hello All,<div><br></div><div>I would like to read multiple files and pro=
cess them.But we could read the first file alone and the rest are skipped f=
rom the while loop. Please correct me where am i missing the logic.Thanks.=
=C2=A0</div><div><br></div><div><div>use strict;</div><div>use warnings;</d=
iv><div>my @files=3Dqw(Alpha.txt Beta.txt Gama.txt);</div><div><br></div><d=
iv>foreach my $file (@files)</div><div>{</div><div>open (my $FH, $file) or =
die &quot;could not open file\n&quot;;</div><div><br></div><div>while( my $=
line =3D &lt;$FH&gt; ){</div><div>#[process the lines &amp; hash constructi=
on.]</div><div>}</div><div>close $FH;</div><div>}</div></div><div><br></div=
><div>Regards,</div><div>Kamal.</div></div>
</blockquote></div>

--94eb2c0c9bf0cdda9005542426d8--
0
chas
7/12/2017 7:51:59 PM
If you wish to terminate execution of a foreach loop without iterating =
over all of the elements (@files, in this case) use the =E2=80=9Clast=E2=80=
=9D statement:

foreach my $file ( @files ) {
 # process file
 open( my $fh, =E2=80=98<=E2=80=98, $file ) or die(=E2=80=A6);
 while( my $line =3D <$fh> ) {
   # process line
 }
 close ($fh) or die( =E2=80=A6 );

 last if (some_condition);
}

If you wish to terminate the foreach loop from inside the while loop, =
you can give the foreach loop a label and use the label in the =
=E2=80=9Clast=E2=80=9D statement. Without an explicit label, =E2=80=9Clast=
=E2=80=9D will terminate the innermost loop (the while loop here):

ALL: foreach my $file ( @files ) {
 # process file
 open( my $fh, =E2=80=98<=E2=80=98, $file ) or die(=E2=80=A6);
 while( my $line =3D <$fh> ) {
   # process line
   last ALL if (some_condition);
 }
 close ($fh) or die( =E2=80=A6 );
}

However, in that case you have skipped the close statement, and the =
close will happen automatically when the file handle $fh goes out of =
scope, but you cannot do any explicit error checking on the close.


> On Jul 12, 2017, at 12:20 PM, perl kamal <kamal.perl@gmail.com> wrote:
>=20
> Hello All,
>=20
> I would like to read multiple files and process them.But we could read =
the first file alone and the rest are skipped from the while loop. =
Please correct me where am i missing the logic.Thanks.=20
>=20
> use strict;
> use warnings;
> my @files=3Dqw(Alpha.txt Beta.txt Gama.txt);
>=20
> foreach my $file (@files)
> {
> open (my $FH, $file) or die "could not open file\n";
>=20
> while( my $line =3D <$FH> ){
> #[process the lines & hash construction.]
> }
> close $FH;
> }
>=20
> Regards,
> Kamal.



Jim Gibson
0
jimsgibson
7/12/2017 9:28:13 PM
On Thu, 13 Jul 2017 00:50:42 +0530
perl kamal <kamal.perl@gmail.com> wrote:

> open (my $FH, $file) or die "could not open file\n";

A quick note: output the file name and error message to have a better
idea of what went wrong.

    open (my $FH, $file) or die "could not open file $file: $!\n";


-- 
Don't stop where the ink does.

	Shawn H Corey
0
shawnhcorey
7/12/2017 10:25:36 PM
--001a11438766239e8105546db297
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Even more readable:

FILE: foreach my $file ( @files ) }
  ...
  last FILE if (some_condition);
  ...
}

Also note that lexical filehandles close when they go out of scope, except
for the most recently "stat"ed file. Perl holds a reference to "the most
recently stat-ed filehandle" in "the solitary underscore" as discussed in t=
he
documentation about -X <https://perldoc.perl.org/functions/-X.html>. The
upshot is that a filehandle used in an expression like "-M $fh" will live
until the next such expression, and the filehandle used in the last such
expression won't close until the end of the program.

David

On Wed, Jul 12, 2017 at 5:28 PM, Jim Gibson <jimsgibson@gmail.com> wrote:

> If you wish to terminate execution of a foreach loop without iterating
> over all of the elements (@files, in this case) use the =E2=80=9Clast=E2=
=80=9D statement:
>
> foreach my $file ( @files ) {
>  # process file
>  open( my $fh, =E2=80=98<=E2=80=98, $file ) or die(=E2=80=A6);
>  while( my $line =3D <$fh> ) {
>    # process line
>  }
>  close ($fh) or die( =E2=80=A6 );
>
>  last if (some_condition);
> }
>
> If you wish to terminate the foreach loop from inside the while loop, you
> can give the foreach loop a label and use the label in the =E2=80=9Clast=
=E2=80=9D
> statement. Without an explicit label, =E2=80=9Clast=E2=80=9D will termina=
te the innermost
> loop (the while loop here):
>
> ALL: foreach my $file ( @files ) {
>  # process file
>  open( my $fh, =E2=80=98<=E2=80=98, $file ) or die(=E2=80=A6);
>  while( my $line =3D <$fh> ) {
>    # process line
>    last ALL if (some_condition);
>  }
>  close ($fh) or die( =E2=80=A6 );
> }
>
> However, in that case you have skipped the close statement, and the close
> will happen automatically when the file handle $fh goes out of scope, but
> you cannot do any explicit error checking on the close.
>
>
> > On Jul 12, 2017, at 12:20 PM, perl kamal <kamal.perl@gmail.com> wrote:
> >
> > Hello All,
> >
> > I would like to read multiple files and process them.But we could read
> the first file alone and the rest are skipped from the while loop. Please
> correct me where am i missing the logic.Thanks.
> >
> > use strict;
> > use warnings;
> > my @files=3Dqw(Alpha.txt Beta.txt Gama.txt);
> >
> > foreach my $file (@files)
> > {
> > open (my $FH, $file) or die "could not open file\n";
> >
> > while( my $line =3D <$FH> ){
> > #[process the lines & hash construction.]
> > }
> > close $FH;
> > }
> >
> > Regards,
> > Kamal.
>
>
>
> Jim Gibson
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--=20
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan

--001a11438766239e8105546db297
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><div>Even more readable:<br><br></div>FILE:=
 foreach my $file ( @files ) }<br>=C2=A0 ...<br></div>=C2=A0 last FILE if (=
some_condition);<br>=C2=A0 ...<br>}<br><br></div>Also note that lexical fil=
ehandles close when they go out of scope, except for the most recently &quo=
t;stat&quot;ed file. Perl holds a reference to &quot;the most recently stat=
-ed filehandle&quot; in &quot;the solitary underscore&quot; as discussed in=
 <a href=3D"https://perldoc.perl.org/functions/-X.html">the documentation a=
bout -X</a>. The upshot is that a filehandle used in an expression like  &q=
uot;-M $fh&quot; will live until the next such expression, and the filehand=
le used in the last such expression won&#39;t close until the end of the pr=
ogram.<br><br></div>David<br></div><div class=3D"gmail_extra"><br><div clas=
s=3D"gmail_quote">On Wed, Jul 12, 2017 at 5:28 PM, Jim Gibson <span dir=3D"=
ltr">&lt;<a href=3D"mailto:jimsgibson@gmail.com" target=3D"_blank">jimsgibs=
on@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If you=
 wish to terminate execution of a foreach loop without iterating over all o=
f the elements (@files, in this case) use the =E2=80=9Clast=E2=80=9D statem=
ent:<br>
<br>
foreach my $file ( @files ) {<br>
=C2=A0# process file<br>
=C2=A0open( my $fh, =E2=80=98&lt;=E2=80=98, $file ) or die(=E2=80=A6);<br>
=C2=A0while( my $line =3D &lt;$fh&gt; ) {<br>
=C2=A0 =C2=A0# process line<br>
=C2=A0}<br>
=C2=A0close ($fh) or die( =E2=80=A6 );<br>
<br>
=C2=A0last if (some_condition);<br>
}<br>
<br>
If you wish to terminate the foreach loop from inside the while loop, you c=
an give the foreach loop a label and use the label in the =E2=80=9Clast=E2=
=80=9D statement. Without an explicit label, =E2=80=9Clast=E2=80=9D will te=
rminate the innermost loop (the while loop here):<br>
<br>
ALL: foreach my $file ( @files ) {<br>
=C2=A0# process file<br>
=C2=A0open( my $fh, =E2=80=98&lt;=E2=80=98, $file ) or die(=E2=80=A6);<br>
=C2=A0while( my $line =3D &lt;$fh&gt; ) {<br>
=C2=A0 =C2=A0# process line<br>
=C2=A0 =C2=A0last ALL if (some_condition);<br>
=C2=A0}<br>
=C2=A0close ($fh) or die( =E2=80=A6 );<br>
}<br>
<br>
However, in that case you have skipped the close statement, and the close w=
ill happen automatically when the file handle $fh goes out of scope, but yo=
u cannot do any explicit error checking on the close.<br>
<div class=3D"HOEnZb"><div class=3D"h5"><br>
<br>
&gt; On Jul 12, 2017, at 12:20 PM, perl kamal &lt;<a href=3D"mailto:kamal.p=
erl@gmail.com">kamal.perl@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hello All,<br>
&gt;<br>
&gt; I would like to read multiple files and process them.But we could read=
 the first file alone and the rest are skipped from the while loop. Please =
correct me where am i missing the logic.Thanks.<br>
&gt;<br>
&gt; use strict;<br>
&gt; use warnings;<br>
&gt; my @files=3Dqw(Alpha.txt Beta.txt Gama.txt);<br>
&gt;<br>
&gt; foreach my $file (@files)<br>
&gt; {<br>
&gt; open (my $FH, $file) or die &quot;could not open file\n&quot;;<br>
&gt;<br>
&gt; while( my $line =3D &lt;$FH&gt; ){<br>
&gt; #[process the lines &amp; hash construction.]<br>
&gt; }<br>
&gt; close $FH;<br>
&gt; }<br>
&gt;<br>
&gt; Regards,<br>
&gt; Kamal.<br>
<br>
<br>
<br>
</div></div><span class=3D"HOEnZb"><font color=3D"#888888">Jim Gibson<br>
<br>
--<br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org">b=
eginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
>beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer" target=3D"_blank">htt=
p://learn.perl.org/</a><br>
<br>
<br>
</font></span></blockquote></div><br><br clear=3D"all"><br>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature">=C2=A0&quot;Debugg=
ing is twice as hard as writing the code in the first place.<br>
 =C2=A0 Therefore, if you write the code as cleverly as possible, you are,<=
br>
 =C2=A0 by definition, not smart enough to debug it.&quot; -- Brian Kernigh=
an<br></div>
</div>

--001a11438766239e8105546db297--
0
dcmertens
7/16/2017 11:36:39 AM
On Sun, 16 Jul 2017 07:36:39 -0400
David Mertens <dcmertens.perl@gmail.com> wrote:

> Also note that lexical filehandles close when they go out of scope

True but you should always explicitly close your files. This gives you
a chance to report any errors it had, have rather than silently
ignoring them.


-- 
Don't stop where the ink does.

	Shawn H Corey
0
shawnhcorey
7/16/2017 12:00:08 PM
--001a113dec505e5b8605546ef3d1
Content-Type: text/plain; charset="UTF-8"

Yes, and I think that gives us *two* reasons to always explicitly close
filehandles. :-)

David

On Sun, Jul 16, 2017 at 8:00 AM, Shawn H Corey <shawnhcorey@gmail.com>
wrote:

> On Sun, 16 Jul 2017 07:36:39 -0400
> David Mertens <dcmertens.perl@gmail.com> wrote:
>
> > Also note that lexical filehandles close when they go out of scope
>
> True but you should always explicitly close your files. This gives you
> a chance to report any errors it had, have rather than silently
> ignoring them.
>
>
> --
> Don't stop where the ink does.
>
>         Shawn H Corey
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan

--001a113dec505e5b8605546ef3d1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Yes, and I think that gives us *two* reasons to alway=
s explicitly close filehandles. :-)<br><br></div>David<br></div><div class=
=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sun, Jul 16, 2017 at 8:0=
0 AM, Shawn H Corey <span dir=3D"ltr">&lt;<a href=3D"mailto:shawnhcorey@gma=
il.com" target=3D"_blank">shawnhcorey@gmail.com</a>&gt;</span> wrote:<br><b=
lockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px =
#ccc solid;padding-left:1ex"><span class=3D"">On Sun, 16 Jul 2017 07:36:39 =
-0400<br>
David Mertens &lt;<a href=3D"mailto:dcmertens.perl@gmail.com">dcmertens.per=
l@gmail.com</a>&gt; wrote:<br>
<br>
&gt; Also note that lexical filehandles close when they go out of scope<br>
<br>
</span>True but you should always explicitly close your files. This gives y=
ou<br>
a chance to report any errors it had, have rather than silently<br>
ignoring them.<br>
<span class=3D"im HOEnZb"><br>
<br>
--<br>
Don&#39;t stop where the ink does.<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Shawn H Corey<br>
<br>
</span><div class=3D"HOEnZb"><div class=3D"h5">--<br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org">b=
eginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
>beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer" target=3D"_blank">htt=
p://learn.perl.org/</a><br>
<br>
<br>
</div></div></blockquote></div><br><br clear=3D"all"><br>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">=C2=A0&quot;Debuggi=
ng is twice as hard as writing the code in the first place.<br>
 =C2=A0 Therefore, if you write the code as cleverly as possible, you are,<=
br>
 =C2=A0 by definition, not smart enough to debug it.&quot; -- Brian Kernigh=
an<br></div>
</div>

--001a113dec505e5b8605546ef3d1--
0
dcmertens
7/16/2017 1:06:29 PM
Reply: