Hi, I do not understand what is happening to $_ (or not) in this while() construct:
my $fname = "while_multi_cond.dat";
my $s1 = "";
open( FP, "<", $fname ) or die "Can't open $fname: $!";
while(<FP>) {
if (/^_start/) {
# This works as expected
# while(<FP> ) {
# if (/^_stop/) { last }
# I'm trying to replace it with this:
while( (<FP>) && (!/^_stop\n/) ) {
# but seems to break the functionality of $_
$s1 .= $_;
}
}
if ($_) { print("$_") }
}
print("\$s1= \n<$s1>");
The data file looks like this:
The file: while_multi_cond.dat
-----
no1
no2
_start
yes 1
yes 2
_stop
no3
no4
----------
In the program output below you can see how $_ retains the value obtained in the outer loop:
~> ./while_multi_cond.pl
parse_data> ./while_multi_cond.pl
no1
no2
_start
$s1=
<_start
_start
_start
_start
_start
_start
>
Why doesn't $_ contain the new lines as they are read in?
Thanks, Howard
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.