Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Printing an array using while loop

by knapper19954 (Initiate)
on Oct 21, 2007 at 20:20 UTC ( #646301=perlquestion: print w/ replies, xml ) Need Help??
knapper19954 has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble printing an array using a while loop. We are required to use the while loop to print it, not the for each like we would normally do. I tried it a few different ways, but I keep getting a endless loop. The array is @array = eq(ball, bat, helmet) I'm new to PERL and could use your help. Thanks!

Comment on Printing an array using while loop
Re: Printing an array using while loop
by moritz (Cardinal) on Oct 21, 2007 at 20:25 UTC
    my $item; while (defined($item = shift @array)){ print $item, "\n"; }

    This destroys @array, though.

    BTW there is no PERL, just perl (the interpreter) and Perl (the language)

    Update: made 0-safe

        Right, the correct solution checks @array, not the returned item.

      Update: made 0-safe

      But still not undef-safe. ;-)

      print shift @array while @array;

      ... which is about the silliest code I've ever written. Sheesh, what's with these arbitrary limitations? :)

      print "Just another Perl ${\(trickster and hacker)},"
      The Sidhekin proves Sidhe did it!

      You've already been shown how to make your code work with all values including undef.

      while (@array) { my $item = shift(@array); print "$item\n"; }

      If you wanted safe code that didn't refer to the array twice, you could use splice in a list assignment.

      while (my ($item) = splice(@array, 0, 1)) { print "$item\n"; }

      The parens on the LHS of the assignment are crucial to force a list context. The result of a list assignment in list context is the number of list elements assigned (no matter if the element(s) are true or false).

      BTW there is no PERL, just perl (the interpreter) and Perl (the language)
      Of course there is. It's usually used as part of a course name. But the course ends up teaching something really different :-)

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Printing an array using while loop
by Joost (Canon) on Oct 21, 2007 at 20:27 UTC
      shouldnt this be really easy?
      my $counter = 0; while($counter <= $#array) { #do your thing $counter++; }
        shouldnt this be really easy?

        Uncountable numbers of fencepost errors in C programs argue otherwise.

Re: Printing an array using while loop
by rminner (Hermit) on Oct 21, 2007 at 22:09 UTC
    A for loop can always be written like a while loop
    my @array = qw{ball bat helmet}; my $i=0; while ($i<=$#array) { print $array[$i],"\n"; $i++; }
Re: Printing an array using while loop
by perlfan (Curate) on Oct 21, 2007 at 22:52 UTC
    or:
    my @array1 = qw/ball bat helmet/; while (@array1) { my $item = pop @array1; print $item; push(@array2,$item); }
    That is if you care about keeping the original contents - otherwise:
    my @array1 = qw/ball bat helmet/; while (@array1) { print pop @array1; }
Re: Printing an array using while loop
by Anonymous Monk on Oct 22, 2007 at 05:52 UTC
    while (1) { print "@array", "\n"; last; }
      In addition to using a "last" in a while loop - which is not good practice, this solution goes against the spirit of the OP's question.
        Well it made me laugh, especially as the "spirit of the OP's question" is obviously homework. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2014-08-28 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (264 votes), past polls