473,787 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$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($innerlin e);
print "$innerline \n";
}
}
}

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

--
Eric Enright /"\
ericAtiptsoftDc om \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Jul 19 '05 #1
6 3690
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.c om (Eric Enright) wrote in message news:<sl******* **********@yama no.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($innerlin e);
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 /"\
ericAtiptsoftDc om \ / 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.ti ptsoft.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($innerlin e);
[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.per l") 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.per l") is defunct. You should use
"comp.lang.per l.misc" instead. Take a look at

perldoc -q usenet


Excellent! Thank you.
--
Eric Enright /"\
ericAtiptsoftDc om \ / 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
9175
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 />Second: 2:3<br>Third: size"; var output = html.match(regEx);
2
2153
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 instance, if I'm working with a table of hotels, I might want a 'select' control as follows <select id="mylistbox" runat="server"> <option value="*">Add New Hotel</option> <option value="1">The Hilton</option> <option value="2">The...
0
1082
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 and is unformatted. I display the text to my user in the listview. The textbox I create logically in the program, and I initialize the correct properties for a normal multiline editor.
6
4093
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 xml_to_tcl_list("&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;&lt;fortune&gt;&lt;saying id=&quot;1&quot; author=&quot;Phil Powell&quot; phrase=&quot;New York is SO Superior!&quot;&gt;&lt;/saying&gt;&lt;saying id=&quot;2&quot; author=&quot;Phil Powell&quot;...
19
3613
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
3
7078
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; NewLineIdr = 0x0000; I need it to use it in my saving procedure because I need to create tabullary text file. But my effort usually results in one or two small
1
11579
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 is invalid. Line 1, position 1. There is an error in XML document (0, 0). &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;MSVBalanceEnquiry xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt; ...
0
1573
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. --> <AffectedBillingComponentsRequest
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.