Re: [NEWBIE] Trivial?
- From: "Mumia W." <paduille.4061.mumia.w+nospam@xxxxxxxxxxxxx>
- Date: Sat, 29 Sep 2007 01:20:16 -0500
On 09/29/2007 12:39 AM, El Bandolero wrote:
I'm solving an exercise studying on a tutorial
The task is to print the (empty or not) lines of a file numbering only the non empty ones.
How the h### is possible that the following code gives the same result if I change line 9 with the opposite condition?
if (!$lines[$i]=="")
1 #!/usr/bin/perl
2 #
Put this line near the top of your program:
use warnings;
3 $file = 'C:\Perl\html\Artistic.txt'; 4 open(PIP, $file); 5 @lines = <PIP>; 6 close(PIP); 7 for ($i=0;$i <= @lines;++$i)
8 {
9 if ($lines[$i]=="")
10 {print $i." ".$lines[$i]};
11 }
The two problems with your program are (1) string comparisons are done with 'eq' not '==' and (2) un-chomped blank lines have a single \n character in them.
This comparison should work better:
if ($lines[$i] ne "\n")
That line alone won't complete the execise. Depending upon the exercise requirements, you may also need another counter ($j). I'll let you figure out if that's the case.
.
- References:
- [NEWBIE] Trivial?
- From: El Bandolero
- [NEWBIE] Trivial?
- Prev by Date: [NEWBIE] Trivial?
- Next by Date: Re: [NEWBIE] Trivial?
- Previous by thread: [NEWBIE] Trivial?
- Next by thread: Re: [NEWBIE] Trivial?
- Index(es):
Relevant Pages
|