Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

using two arrays to make a table

by Bongo (Initiate)
on Jun 18, 2002 at 16:22 UTC ( #175416=perlquestion: print w/replies, xml ) Need Help??

Bongo has asked for the wisdom of the Perl Monks concerning the following question:

I have two arrays. The first contains a list of headings for a table. The second contains a list of names that would go under the headings from the first array. I have to print the first heading, print the first ten names, print the second heading, print the second set of ten names, and so on. Here is the code I have so far:


foreach $heading (@headings) { print $heading; for ($count == 0;$count < 10;$count++) { $name = shift(@names); print $name; } }

My problem is...it prints the first heading and first ten names fine...then it finishes printing all the headings with no names. Like it only goes through the 'for' loop once. Can someone explain what i'm doing wrong?

Replies are listed 'Best First'.
(jeffa) Re: using two arrays to make a table
by jeffa (Bishop) on Jun 18, 2002 at 17:00 UTC
    I used your code with the following arrays:
    my @headings = map { "Head$_" } (1..5); my @names = qw( one two three un deux trois eins zwei drei uno dos tres een twee drie );
    I changed $count < 10 to $count < 3, fixed the == typo (;)), and the code produced this output:
    Head1onetwothreeHead2undeuxtroisHead3einszweidrei ...
    Not very useful. :P

    So now i take the liberty of showing you another way to do this:

    use strict; # please my @headings = map { "Head$_" } (1..5); my @names = ( [qw(one two three)], [qw(un deux trois)], [qw(eins zwei drei)], [qw(uno dos tres)], [qw(een twee drie)], ); # horizontal for (0..$#headings) { print "$headings[$_]: "; print join(', ',@{$names[$_]}),"\n"; } # vertical printf("%10s %10s %10s\n",@headings); printf("%10s %10s %10s\n",@$_) for @names;
    Hope this helps, and please, next time let us know what your arrays look like as well as your desired output.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: using two arrays to make a table
by thelenm (Vicar) on Jun 18, 2002 at 16:28 UTC
    If you change $count == 0 to $count = 0, your problem should go away (I hope).

    -- Mike

    --
    just,my${.02}

Re: using two arrays to make a table
by mfriedman (Monk) on Jun 18, 2002 at 17:33 UTC
    Your inner loop confuses me. For one, you're using the conditional == operator when you probably mean the assignment = operator. Secondly, why are you using a C-style for loop on the inside when you can just say

    foreach my $heading(@headings) { print $heading; foreach my $name (@names) { print $name; } }
      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.

        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

        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.
Re: using two arrays to make a table
by Abigail-II (Bishop) on Jun 19, 2002 at 13:25 UTC
    Had you turned warnings on, Perl would have told you what you did wrong.

    Always, always turn warnings on, unless you know what you are doing.

    Abigail

Re: using two arrays to make a table
by LanceDeeply (Chaplain) on Jun 18, 2002 at 19:06 UTC
    hi bongo-

    alot of the monks already suggested good ways to do this.

    but, if you're still wondering what's going on in your code,
    the shift operator removes elements from @name.
    that means the second time through the loop, @name is empty.
    and nothing is printed.
      the second time through the loop, @name is empty.

      Not necessarily... since Bongo didn't mention how the arrays get filled in the first place, @names could have a lot more elements than @headings. But it may be worthwhile for Bongo to check that this is the case.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2021-07-31 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?