Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: using two arrays to make a table

by demerphq (Chancellor)
on Jun 18, 2002 at 18:32 UTC ( #175448=note: print w/replies, xml ) Need Help??


in reply to Re: using two arrays to make a table
in thread using two arrays to make a table

Secondly, why are you using a C-style for loop on the inside when you can just say

I imagine because he has 1 list of names, and he wants 10 at a time. Your code doesnt do that at all. (Sorry ;-)

But i agree with you that the 3 arg for is a bad call, especially considering his mistake with the initializer. Personally i would do this:

foreach $heading (@headings) { print $heading; print "".shift(@names) for 1..10; # the "". might not be needed, cant + remember :-) }
But its a little hard to say considering we dont know the constraints. For instance are there for sure a sufficient number of names?

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

Replies are listed 'Best First'.
Re: using two arrays to make a table
by Abigail-II (Bishop) on Jun 19, 2002 at 13:31 UTC
    Well, I don't know what's so bad about a "C-style for loop" (it isn't there so people can complain about its usage), but if you're going to be picky, don't replace it with another wimpy for loop. Just use some power:
    print $_, splice @names => 0, 10 for @headings;

    Abigail

      I don't know what's so bad about a "C-style for loop"

      There are two problems with C style for loops. First they are prone to fencepost errors. In fact programmers that dont have a C/Java background get them wrong an awful lot, for instance I come from a Pascal background, and despite being a reasonably good programmer often make mistakes when I use them. (hence I dont use them if I can get away with it).

      Second is that they are marginally less efficient than perl style for loops. Granted this isnt the best argument, but combined with the former its IMO pretty good reasoning that avoiding them is a good way to reduce error, speed up your code and leave a more maintainable product (the later is excellent justifiction IMO).

      it isn't there so people can complain about its usage

      No its there to make C programmers feel more at home. Presumably a C programmer has already learned the subtleties of this style of loop and so will not make the common errors as often. This doesnt mean that it should be recommended or even overlooked, especially as the alternative is so much superior.

      As for your solution ++. splice() is one of those tools that I have yet to become entirely comfortable with. I suppose its something I should try to use more often.

      Good post, thanks.

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

Re: Re: Re: using two arrays to make a table
by Bongo (Initiate) on Jun 19, 2002 at 02:51 UTC
    Hi it's Bongo! Just wanted to tell you that your one line of code fixed the problem. And thanks to everyone else who answered...you all gave me things to ponder.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-08-03 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (40 votes). Check out past polls.

    Notices?