[NEWBIE] Trivial?

E

El Bandolero

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 #
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 }
 
M

Mumia W.

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.
 
J

John W. Krahn

El said:
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 #
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 }

How is it possible? The expressions $lines[$i] and !$lines[$i] are both
*numerically* equal to the string "", unless the contents of $lines[$i] start
with numerical digits. In Perl, any non-numeric string has the numeric value
of zero so 0 == 0 is always true.

You should enable warnings and perl would have informed you that you are using
numerical comparison on strings instead of numbers.

#!/usr/bin/perl
use warnings;
use strict;

my $file = 'C:/Perl/html/Artistic.txt';

open my $PIP, '<', $file or die "Cannot open '$file' $!";

while ( <$PIP> ) {
print $. - 1, " $_" if /^$/;
}

close $PIP;

__END__




John
 
M

Mintcake

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.

What you probably want is more like

if ($lines[$i] =~ /\S/)

After all, if it looks blank it is blank (especially to the tutor)
 
M

Mintcake


Apologies - I probably misinterpreted the original posting. nl -bt is
precisely what is required according to requirement - it's just that
the original script was making no attempt whatsoever to print the
blank lines
 
W

William James

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.

awk 'NF{printf "%d ", NR} 1' file
 
C

Charlton Wilbur

M> On Sep 30, 7:37 am, all mail refused

M> That doesn't do the job - it still prints the empty lines it
M> just doesn't number them

Er, that's exactly what the OP asked for. "print the (empty or not)
lines of a file numbering only the non empty ones."

Charlton
 
M

Mintcake

M> On Sep 30, 7:37 am, all mail refused


M> That doesn't do the job - it still prints the empty lines it
M> just doesn't number them

Er, that's exactly what the OP asked for. "print the (empty or not)
lines of a file numbering only the non empty ones."

Charlton

I think you missed my earlier apology
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top