473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3675
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. --> ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.