[NEWBIE] Trivial?

[NEWBIE] Trivial?

am 29.09.2007 07:39:57 von 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 = ;
6 close(PIP);
7 for ($i=0;$i <= @lines;++$i)
8 {
9 if ($lines[$i]=="")
10 {print $i." ".$lines[$i]};
11 }


--
ciao
Bando

Re: [NEWBIE] Trivial?

am 29.09.2007 08:20:16 von paduille.4061.mumia.w+nospam

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 = ;
> 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.

Re: [NEWBIE] Trivial?

am 29.09.2007 08:53:30 von Dummy

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 #
> 3 $file = 'C:\Perl\html\Artistic.txt';
> 4 open(PIP, $file);
> 5 @lines = ;
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: [NEWBIE] Trivial?

am 30.09.2007 02:37:18 von unknown

Post removed (X-No-Archive: yes)

Re: Trivial?

am 02.10.2007 08:44:25 von Mintcake

On Sep 30, 7:37 am, all mail refused
wrote:
> On 2007-09-29, 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.
>
> nl -bt
>
> --
> Elvis Notargiacomo master AT barefaced DOT cheekhttp://www.notatla.org.uk/goen/

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

Re: Trivial?

am 02.10.2007 08:47:18 von Mintcake

On Sep 29, 1:20 pm, "Mumia W." +nos...@earthlink.net> wrote:
> 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 = ;
> > 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)

Re: Trivial?

am 02.10.2007 09:44:22 von Mintcake

On Sep 30, 7:37 am, all mail refused
wrote:
> On 2007-09-29, 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.
>
> nl -bt
>
> --
> Elvis Notargiacomo master AT barefaced DOT cheekhttp://www.notatla.org.uk/goen/

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

Re: Trivial?

am 02.10.2007 14:02:03 von William James

On Sep 29, 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.
>

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

Re: Trivial?

am 02.10.2007 16:08:16 von Charlton Wilbur

>>>>> "M" == Mintcake writes:

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

>> On 2007-09-29, 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.
>>
>> nl -bt
>>
>> -- Elvis Notargiacomo master AT barefaced DOT
>> cheekhttp://www.notatla.org.uk/goen/

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


--
Charlton Wilbur
cwilbur@chromatico.net

Re: Trivial?

am 03.10.2007 02:32:48 von Mintcake

On Oct 2, 9:08 pm, Charlton Wilbur wrote:
> >>>>> "M" == Mintcake writes:
>
> M> On Sep 30, 7:37 am, all mail refused
> M>
> M> wrote:
>
> >> On 2007-09-29, 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.
> >>
> >> nl -bt
> >>
> >> -- Elvis Notargiacomo master AT barefaced DOT
> >> cheekhttp://www.notatla.org.uk/goen/
>
> 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
>
> --
> Charlton Wilbur
> cwil...@chromatico.net

I think you missed my earlier apology