Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop deleting first line in file?

Status
Not open for further replies.

ZsN1Gman

Programmer
Aug 22, 2002
17
US
I wrote this script in order to change multiple instances of text, for instance to change the location of images in a HTML doc. However, when the file is being read in and assigned to the array, it is removing the first line in the file? I'm sure it's a simple solution, I'm just not experienced enough to see it.

Any help is appreciated, the source is listed below.

Thanks, Jim

Code:
#!/usr/local/bin/perl -w 

$counter = 1;

while (<*.inc>) {  # < -- Enter the file name here. 
$page = &quot;$_&quot;;

#Opening file to save contents
open (PAGE, &quot;$page&quot;) || die &quot;Can't open $page $!\n&quot;;

#saving contents 
while(<PAGE>) 
{ 
@contents = (<PAGE>); 
} 

close PAGE; 

print &quot;Content-type: text/html\n\n&quot;; 
print <pre>; 

#processing file contents 
foreach $item (@contents) 
{                     
$item =~ s!<!<!g; # < -- change regex as needed 
$item =~ s!>!>!g; 
@converted =(@converted, $item); 
print &quot;$item&quot;; 
$counter ++;             
} 

print &quot;</pre>&quot;; 

#opening page to save changes 
open (PAGE, &quot;>$page&quot;) || die &quot;Can't open $page $!\n&quot;;
 
foreach $line (@contents) 
{ 
print PAGE &quot;$line&quot;; 
} 

close PAGE; 

@contents = (); #removing array 
@converted = (); #contents 

print &quot;\&quot;$page\&quot;:\nProcessed Successfully...\n\n&quot;; 
}
 
You think that's strange, check this (thread219-341357) out. Note, in particular, the explaination in the last post, by dchoulette.

jaa
 
::pOP:: as I firmly yank the pacifier from my mouth..

I appreciate the redirect.. I can't say his situation was fully comparable with mine, but useful at any rate.

I simply changed:

while(<PAGE>)
{
@contents = (<PAGE>);
}

To read:

while(<PAGE>)
{
$line = $_;
@contents = (@contents, $line);
}

This solved it, Thanks.

... Now where did I set that pacifier??
 
Alternatives to that are
Code:
while(<PAGE>) {

    push @contents, $_;
}
Or
Code:
# without the while loop
@contents = <PAGE>;
# This will put ALL lines, including the first, into @contents

jaa




 
You could simply do:
Code:
@contents = <PAGE>;
As @contents is an array, <PAGE> is not evaluated in scalar context (as it is in the while) returning only one line, but in list context and returns the whole file as an array of lines.
This simple assignment replace your whole loop.

 
Brilliant!! And I'm speaking to both of you.

@contents = <PAGE>; # <- could this be any more logical??

Basically my original code was good, I just screwed it up by throwing it in the loop.

Though in a sense my solution was better in that it appears more &quot;magical&quot;, easier to impress that perl illiterate boss.

Being serious now, I still don't understand why the original code deleted the top line..

while(<PAGE>)
{
@contents = (<PAGE>);
}

Logically if you have a 100-line file, this would assign the entire page to @contents 100 times? Of course that is 99 times more than necessary, but what would cause the top line to be lost?

As you can see I just barely have my foot in the door when it comes to perl.

I really appreciate your replies.. Thanks!!
 
&quot;Being serious now, I still don't understand
why the original code deleted the top line..&quot;

You've already read the first line into $_
when you say &quot;while (<PAGE>)&quot;. The line inside
the loop reads the remainder of the file, lines 2
through whatever, into the array @contents. PAGE
is now exhausted, so the loop terminates, and the
first line which was read into $_ is effectively
thrown away.

&quot;Logically if you have a 100-line file, this would
assign the entire page to @contents 100 times?&quot;

No. The loop terminates after one iteration, because
there's nothing more to read.

 
You are absolutely right, not that I doubted you.

while(<PAGE>)
{
@contents = (<PAGE>);
}

I tried it, this iterated once, not that I doubted you :)

I had assumed that <PAGE> contained the contents of page forever, but now I see I'm wrong. It appears that after each line in page is read it is discarded. I proved it to myself with the following..

$counter = 0;
@contents = <PAGE>;
while (<PAGE>)
{
counter++;
}
print &quot;$counter&quot;;

The counter remained at &quot;0&quot;, so I assume the while loop was skipped because <PAGE> was empty.

I appreciate you pointing this out to me, as it could have been a long time before I figured it out on my own.

I have allot to learn, thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top