Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What am I doing wrong?!

Reply
Thread Tools

What am I doing wrong?!

 
 
David K. Wall
Guest
Posts: n/a
 
      10-30-2003
Jürgen Exner <> wrote:

> Tore Aursand wrote:
>> On Thu, 30 Oct 2003 02:12:54 -0800, Tony Walker wrote:
>>> I'm now trying
>>> for (@entry)
>>>

>> Use 'foreach';
>> foreach ( @array )

>
> What exactly would be the difference between 'for' and 'foreach'
> (besides the additional 4 letters)?


It's a bit more explicit about what it's doing? perlsyn gives
'readability' as the justification for the existence of 'foreach', but
that's arguable, and I don't really see the need for it. 'foreach' is
a bit of dead yeast left in the Perl beer, IMHO; forget the yeast and
enjoy the flavor.

--
David Wall
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      10-31-2003
On Thu, 30 Oct 2003 18:10:17 +0000, Jürgen Exner wrote:
>>> I'm now trying
>>> for (@entry)


>> Use 'foreach';
>> foreach ( @array )


> What exactly would be the difference between 'for' and 'foreach'
> (besides the additional 4 letters)?


I would say readability, plus that it simply works better than writing
'for (my $i = 0..@array)' (if you had bothered quoting the relevant text
from my post.


--
Tore Aursand <>
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-31-2003

Tore Aursand <> wrote:
> On Thu, 30 Oct 2003 18:10:17 +0000, Jürgen Exner wrote:
> >>> I'm now trying
> >>> for (@entry)

>
> >> Use 'foreach';
> >> foreach ( @array )

>
> > What exactly would be the difference between 'for' and 'foreach'
> > (besides the additional 4 letters)?

>
> I would say readability,


Debatable, but yes.

> plus that it simply works better than writing
> 'for (my $i = 0..@array)'


No. The problem there is nothing to do with for/foreach. foreach can
in every single case be replaced with for.

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      10-31-2003
On Fri, 31 Oct 2003 07:23:24 +0000, Ben Morrow wrote:
>> plus that it simply works better than writing
>> 'for (my $i = 0..@array)'


> No. The problem there is nothing to do with for/foreach. foreach can
> in every single case be replaced with for.


I know that, but look at the actual code. Maybe I'm misunderstanding
something here, but have tried this?

my @array = qw( a b c d e f );
for ( my $i = 0 .. @array ) {
print $i . ' = ' . $array[ $i ] . "\n";
}

Doesn't look nice to me.


--
Tore Aursand <>
 
Reply With Quote
 
Jeff Boes
Guest
Posts: n/a
 
      10-31-2003
Tony Walker wrote:
> my $LINE = @entry[$i];
> if ($CUR =~/$LINE/){
> print $CUR;
> }


Consider "$CUR =~ /\Q$LINE\E/" here; if the text in $LINE contains any
regular expression meta-characters (like "."), your match may not work
the way you want it to.

--
Jeff Boes vox 269.226.9550 ext 24
Database Engineer fax 269.349.9076
Nexcerpt, Inc. http://www.nexcerpt.com
...Nexcerpt... Extend your Expertise

 
Reply With Quote
 
Tony Walker
Guest
Posts: n/a
 
      11-03-2003
Jeff Boes <> wrote in message news:< ws.com>...
> Tony Walker wrote:
> > my $LINE = @entry[$i];
> > if ($CUR =~/$LINE/){
> > print $CUR;
> > }

>
> Consider "$CUR =~ /\Q$LINE\E/" here; if the text in $LINE contains any
> regular expression meta-characters (like "."), your match may not work
> the way you want it to.


Thanks for all the input Gentlemen. Since posting my rather 'useless'
error description I have cracked the problem and the script I
originally posted this message about is running nicely without
problems. Nice to have found such a rich source of information...

Tony
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-03-2003
Tore Aursand <> wrote in comp.lang.perl.misc:
> On Fri, 31 Oct 2003 07:23:24 +0000, Ben Morrow wrote:
> >> plus that it simply works better than writing
> >> 'for (my $i = 0..@array)'

>
> > No. The problem there is nothing to do with for/foreach. foreach can
> > in every single case be replaced with for.

>
> I know that, but look at the actual code. Maybe I'm misunderstanding
> something here, but have tried this?
>
> my @array = qw( a b c d e f );
> for ( my $i = 0 .. @array ) {
> print $i . ' = ' . $array[ $i ] . "\n";
> }
>
> Doesn't look nice to me.


No, but it doesn't work with foreach either.

While "for" and "foreach" are really synonyms, there are still two
entirely different loop constructs they can *both* introduce. The
difference is in the syntax of what follows for/foreach.

Anno
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-03-2003
On Mon, 03 Nov 2003 10:51:27 +0000, Anno Siegel wrote:
>> my @array = qw( a b c d e f );
>> for ( my $i = 0 .. @array ) {
>> print $i . ' = ' . $array[ $i ] . "\n";
>> }
>>
>> Doesn't look nice to me.


> No, but it doesn't work with foreach either.


I never said that. I just said that one should use...

foreach ( @array ) {
...
}

....instead of the code above. I know that 'for' and 'foreach' do excactly
the same.


--
Tore Aursand <>
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-03-2003
Tore Aursand <> wrote in comp.lang.perl.misc:
> On Mon, 03 Nov 2003 10:51:27 +0000, Anno Siegel wrote:
> >> my @array = qw( a b c d e f );
> >> for ( my $i = 0 .. @array ) {
> >> print $i . ' = ' . $array[ $i ] . "\n";
> >> }
> >>
> >> Doesn't look nice to me.

>
> > No, but it doesn't work with foreach either.

>
> I never said that. I just said that one should use...
>
> foreach ( @array ) {
> ...
> }


But the code above isn't just bad, or hard to read, it's *wrong*. It
doesn't loop over the array. Instead it assigns the result of "scalar
0 .. @array" to $i (once), assigns the result to $_, and executes the
loop body (once).

(Apart from that, you'd want $#array instead of @array, but that doesn't
matter anymore.)

Anno
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-03-2003
On Mon, 03 Nov 2003 12:37:27 +0000, Anno Siegel wrote:
> [...]
> But the code above isn't just bad, or hard to read, it's *wrong*.


Excactly my point.


--
Tore Aursand <>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recommendation doing co-simulation between c/c++ with vhdl Carson VHDL 0 10-05-2005 07:40 PM
What is VS Doing to my Code??? downs.matt@gmail.com ASP .Net 1 09-02-2005 11:38 AM
Why is Mozilla doing this ... and how to fix it Joe S. Firefox 7 06-07-2005 04:12 AM
VB6/VB.Net Programming Question - what am i doing wrong? Salisha Khan ASP .Net 1 08-01-2003 01:55 PM
What am I doing wrong? Keith R. Williams VHDL 4 07-15-2003 03:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57