472,967 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 software developers and data experts.

$var = <LINE> ??

Could someone please tell me why this code doesn't work?

----- begin "line.pl" -----
#!/usr/bin/perl

# output i'm expecting:
#
#-----begin-----
#foreach $outerline (<FIN>)
#{
# if ($outerline =~ /outerline/)
# {
# my $innerline;
#-----end-----
#
# ouput i get:
# <insert millions of empty lines here>
#
# i've tracked the problem down to the "$innerline = <FIN>;" line, however
# http://www.perldoc.com/perl5.8.0/pod/func/readline.html
# indicates this is valid??
use strict;

# assuming this source file is called "line.pl"
open(FIN, "<line.pl")
or die "unable to open myself for reading!??\n";

my $outerline;
foreach $outerline (<FIN>)
{
if ($outerline =~ /outerline/)
{
my $innerline;
while (!($innerline =~ /innerline/))
{
$innerline = <FIN>;
chomp($innerline);
print "$innerline\n";
}
}
}

close(FIN);
----- end "line.pl -----
Thank you kindly for any help in advance.

--
Eric Enright /"\
ericAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Jul 19 '05 #1
6 3653
Eric Enright wrote:
Could someone please tell me why this code doesn't work?

foreach $outerline (<FIN>)


That says to read the entire file in all at once, then process it a line
at a time. There is nothing left for the inner loop to read.

You want to use a while(){} instead of a foreach(){}.
-Joe
Jul 19 '05 #2
er**@tiptsoft.com (Eric Enright) wrote in message news:<sl*****************@yamano.tiptsoft.com>...
Could someone please tell me why this code doesn't work?

----- begin "line.pl" -----
#!/usr/bin/perl

# output i'm expecting:
#
#-----begin-----
#foreach $outerline (<FIN>)
#{
# if ($outerline =~ /outerline/)
# {
# my $innerline;
#-----end-----
#
# ouput i get:
# <insert millions of empty lines here>
#
# i've tracked the problem down to the "$innerline = <FIN>;" line, however
# http://www.perldoc.com/perl5.8.0/pod/func/readline.html
# indicates this is valid??
use strict;

# assuming this source file is called "line.pl"
open(FIN, "<line.pl")
or die "unable to open myself for reading!??\n";

my $outerline;
foreach $outerline (<FIN>)
{
if ($outerline =~ /outerline/)
{
my $innerline;
while (!($innerline =~ /innerline/))
{
$innerline = <FIN>;
chomp($innerline);
print "$innerline\n";
}
}
}

close(FIN);
----- end "line.pl -----
Thank you kindly for any help in advance.


Eric,

foreach $outerline (<FIN>)

this line is reading every line from FIN and storing them in a
temporary list before beginning the first iteration. so there is
nothing left in FIN to read with this line:

$innerline = <FIN>;

instead of the foreach loop use a while loop:

while( $outerline = <FIN> )

this would only read one line before each loop iteration.
alf
Jul 19 '05 #3
Joe Smith wrote:
Eric Enright wrote:
Could someone please tell me why this code doesn't work?

foreach $outerline (<FIN>)


That says to read the entire file in all at once, then process it a line
at a time. There is nothing left for the inner loop to read.

You want to use a while(){} instead of a foreach(){}.
-Joe


Ah, thanks Joe and Alf, I was under the impression that it would
only take one line per iteration. I got things working nicely
now.

Thanks!
--
Eric Enright /"\
ericAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Jul 19 '05 #4
Eric Enright wrote on Wed, 07 Jul 2004 00:59:37 GMT in article
<sl*****************@yamano.tiptsoft.com>:
Could someone please tell me why this code doesn't work?

----- begin "line.pl" -----
#!/usr/bin/perl

# output i'm expecting:
#
#-----begin-----
#foreach $outerline (<FIN>)
#{
# if ($outerline =~ /outerline/)
# {
# my $innerline;
#-----end-----
#
# ouput i get:
# <insert millions of empty lines here>
#
# i've tracked the problem down to the "$innerline = <FIN>;" line,
That's not where the real problem lies, though!
however
# http://www.perldoc.com/perl5.8.0/pod/func/readline.html
# indicates this is valid??
use strict;
use warnings;

ALWAYS use warnings! They would have told you something
important:

Use of uninitialized value in:
(1) pattern match (m//) at line 33
(2) scalar chomp at line 36

repeated a zillion times. (Of course, if you add "use
warnings;", your line numbers will be different.)

# assuming this source file is called "line.pl"
open(FIN, "<line.pl")
or die "unable to open myself for reading!??\n";
Better:
or die "unable to open myself for reading: $!";

"$!" isn't comic-book-style cursing. It gives useful
information.


my $outerline;
foreach $outerline (<FIN>)
HERE is your real problem! foreach() will evaluate <FIN> in
list context, which means that the entire file will be read
into memory, and *then* $outerline will be assigned one line at
a time.
{
if ($outerline =~ /outerline/)
{
my $innerline;
Now $innerline = undef.
while (!($innerline =~ /innerline/))
[This is line 33.] First time around, $innerline = undef, and
undef doesn't match 'innerline', so this will loop. You will
also get warning (1).
{
$innerline = <FIN>;
We have already read past end-of-file, so <FIN> returns undef.
chomp($innerline);
[This is line 36.] Since $innerline = undef, this will give
you warning (2). print "$innerline\n";
}
The while loop restarts, and since $innerline is still
undefined, it'll continue forever and ever...
}
}

close(FIN);
----- end "line.pl -----
Thank you kindly for any help in advance.


I'm sorry I couldn't provide any help in advance. :-) Hope
this helps!

PS: This group ("comp.lang.perl") is defunct. You should use
"comp.lang.perl.misc" instead. Take a look at

perldoc -q usenet


Jul 19 '05 #5
Thomas Wasell wrote:
Eric Enright wrote on Wed, 07 Jul 2004 00:59:37 GMT in article
use strict;
use warnings;

ALWAYS use warnings! They would have told you something
important:

Use of uninitialized value in:
(1) pattern match (m//) at line 33
(2) scalar chomp at line 36

repeated a zillion times. (Of course, if you add "use
warnings;", your line numbers will be different.)


Duly noted.
my $outerline;
foreach $outerline (<FIN>)


HERE is your real problem! foreach() will evaluate <FIN> in
list context, which means that the entire file will be read
into memory, and *then* $outerline will be assigned one line at
a time.


I was under the impression that this was an efficient way to run
through a file line by line, without a lot of buffering. I read
this somewhere for a method of running through a million line
file without buffering it all first. Perhaps it was incorrect,
or more likely, I misinterpreted it/munged it in my memory.

I understand now though, and have things working well.
Thank you kindly for any help in advance.


I'm sorry I couldn't provide any help in advance. :-) Hope
this helps!


;-)
PS: This group ("comp.lang.perl") is defunct. You should use
"comp.lang.perl.misc" instead. Take a look at

perldoc -q usenet


Excellent! Thank you.
--
Eric Enright /"\
ericAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Jul 19 '05 #6
Eric Enright wrote:
foreach $outerline (<FIN>)


HERE is your real problem! foreach() will evaluate <FIN> in
list context, which means that the entire file will be read
into memory, and *then* $outerline will be assigned one line at
a time.


I was under the impression that this was an efficient way to run
through a file line by line, without a lot of buffering. I read
this somewhere for a method of running through a million line
file without buffering it all first.


The way to go through a file line by line without a lot of buffering is
while(<FIN>) {...}
or
while(defined $outerline=<FIN>) {...}
and not use foreach().
-Joe
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aliensite | last post by:
My code is too greedy, how can it be fixed? Here is my code: Desired output - First:,Second:,Third: <br> <script type="text/javascript"> var regEx = /*?:/g; var html = "<br>First:ratio<br...
2
by: COHENMARVIN | last post by:
I'm leafing through a big book on asp.net, and I don't see any way to the following: 1. bind values and text to a dropdown 2. Also make the first line of the dropdown say something different. For...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
6
by: comp.lang.php | last post by:
I am trying to pipe in some auto-generated PHP into php.exe, to no avail: catch {exec echo '<? if (@is_file("./functions.inc.php")) { require_once("./functions.inc.php"); echo...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
3
by: MIUSS | last post by:
Hello everyone! I got a problem with creating new line... I tried this: lstrcpy(NewLineIdr, TEXT("\r\n")); I already tried this: NewLineIdr = 0x000D; NewLineIdr = 0x000A;...
1
by: ayemyat | last post by:
Hi All, I have a remoting service which consumes the web service in another server. I have the following exception throw by the web service. System.Xml.XmlException: The data at the root level...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.