Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Breaking output lines into N-element chunks

by demerphq (Chancellor)
on Apr 12, 2002 at 13:49 UTC ( [id://158573]=note: print w/replies, xml ) Need Help??


in reply to Re: Breaking output lines into N-element chunks
in thread Breaking output lines into N-element chunks

How about a C-style loop?

Whats wrong with a perl loop? Why use a loop form that is inclined toward error when you dont have to?

for my $i (0..@vars-1){ }

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

Replies are listed 'Best First'.
Re(3): Breaking output lines into N-element chunks
by FoxtrotUniform (Prior) on Apr 12, 2002 at 15:27 UTC
      Whats wrong with a perl loop? Why use a loop form that is inclined toward error when you dont have to?
      for my $i (0..@vars-1){ }

    Oww oww oww! Make the hurting stop! Avoiding confusion between number-of-elements and last-valid-index is exactly why Perlish for loops are (usually) safer than C for loops. If you're going to be using $i as an array index, at least loop from 0 to $#vars instead of mucking about with length-1. IMO, your loop is more error-prone than a C-style loop, because eventually someone will forget the -1.

    --
    Good luck, tilly
    :wq

      at least loop from 0 to $#vars instead of mucking about with length-1

      Actually I used to do this. But I have started getting out of the habit as I understand $#array has been removed from Perl6.

      Do you really think its that confusing? @array is a one based number, and indexes are zero based. Seems pretty reasonable to me... And I have to say that definately _dont_ agree that its more error prone than a C-Style for loop.

      :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

        But I have started getting out of the habit as I understand $#array has been removed from Perl6.

        Perl 6 isn't Perl 5.x, it's a new language that happens to be a lot like Perl 5. You will have to re-code things anyway. There will be a Perl 5 to 6 converter, so $#foo will be converted to @foo.end or @foo.last (I forgot which one it was), which is probably better than (@foo.length - 1).

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

      at least loop from 0 to $#vars instead of mucking about with length-1

      Actually I used to do this. But I have started getting out of the habit as I understand $#array has been removed from Perl6.

      Do you really think its that confusing? @array is a one based number, and indexes are zero based. Seems pretty reasonable to me...

      :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://158573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found