Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Cant use array with numbers??

by RobinVossen (Initiate)
on Sep 18, 2007 at 08:18 UTC ( [id://639576]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Cant use array with numbers??
by Corion (Patriarch) on Sep 18, 2007 at 08:21 UTC

    Read for.

    Normally, you want to provide C++ and Perl with syntactically correct code though.

    The way of performing a loop over a list of array elements in Perl is usually:

    foreach (@Array) { next if $_ eq "DontPrintMe"; print $_; };

      I like to put an explicit name in instead of $_, like so:

      for my $value (@array) { do_something_with($value); }

      Going back to the C/C++ style comparison, I also think $value is more readable than $array[$i], but that's a matter of opinion and/or of naming things well.

      But remember that $value is not available outside the for loop. If you're worried about knowing where you are in the array, I've found I don't need to worry most of the time or I can just create put 'my $i = 0;' before the loop and do $i++ each time round.

      How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
      Robot Tourist, by Ten Benson

Re: Cant use array with numbers??
by GrandFather (Saint) on Sep 18, 2007 at 11:30 UTC

    What does "powerful" mean in this context? In Perl I'd be tempted to:

    $_ ne "DontPrintMe" and print for @Array;

    but that may be too much power for the masses who may be happier with:

    for (@Array) { next if $_ eq "DontPrintMe"; print; }

    although for someone from a C/C++ background that may still be a little blinding and would expect to see the rather more Cish:

    for (my $i = 0; $i < @Array; ++$i) { print $Array[$i] unless $Array[$i] eq "DontPrintMe"; }

    which of course is still not toned down to a C/C++ level of power, but possibly looks familiar enough not to cause offense.


    DWIM is Perl's answer to Gödel
Re: Cant use array with numbers??
by bruceb3 (Pilgrim) on Sep 18, 2007 at 08:21 UTC
    There are a couple of bugs in the for loop.

    You probably want something like;

    for($i=0; $i < @Array; $i++) { ... }
    The for loop can be used in Perl the same way as you would in C. Of course the for loop in Perl can be used in the other ways too.

      We have been encouraging the following syntax:

      for my $i (0..$#Array) { ... }

      It's just as efficient, and much more readable.

        That is fair enough. I wasn't trying to recommend a better way, only answer the question asked.
Re: Cant use array with numbers??
by mzedeler (Pilgrim) on Sep 18, 2007 at 09:06 UTC
    I suppose that you're asking for a snipplet to iterate over all elements in an array. Thats certainly possible in (at least) two common variations:
    for(my $i=0; $i < scalar(@array); $i++) { print $array[$i], "\n"; }
    ...or just omitting the index and using values straight from the array:
    for( @array ) { print $_, "\n"; }
Re: Cant use array with numbers??
by swampyankee (Parson) on Sep 18, 2007 at 14:45 UTC

    Goodness, there are so many ways of doing that sort of thing in Perl…

    #!perl use strict; use warnings; my @array; @array = <DATA>; print (grep { !/^DontPrintMe$/} @array); __DATA__ print me! no, print me! DontPrintMe Look out for open manholes Do print me!

    Incidentally, Perl is Turing-complete, as are quite a few other programming languages. Stating that Perl is "almost as powerfull[sic] as C++" is, first, incorrect, and, second, could be construed as insulting, as it seems to imply that "real programmers don't use Perl."


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

Re: Cant use array with numbers??
by talexb (Chancellor) on Sep 18, 2007 at 15:39 UTC

    I would normally see this as a soluition:

    foreach ( @array ) { print $_ unless ( $_ eq "DontPrintMe" ); }
    This avoids the (in my mind) messy next which really isn't needed in this very small loop, and it reads well syntactically: For each element in the array, print the element unless it's a "Dont Print Me". which is only a small jump from the ideal Print each element in the array unless it's a "Don't Print Me".

    You already have a couple of different ways to do it in this thread -- but you could also use map in a void context; I'll leave that for you to discover (and avoid).

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-29 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found