Actually, to be mildly heretical, I don't think perl offers more "extra" ways of doing things than many other languages.

Somebody _had_ to call you on this. :-)

I can think of a disturbing number of ways to iterate over the elements in an array in perl. In Pascal I can only think of a few, likewise for VB, and similarly for C/C++ (I mention this one last because I know it the least well and so might be suprised.)

Without using procedure calls I can think of the following notationally distinct means (and thats just at the moment, no doubt there are more, and i'm even ignoring the 'for' eq 'foreach' aspect that adds even more)

# 1. ... map { ... } @array; # 2. ... grep { ... } @array; # 3. for my $item (@array) { ... } # 4. ... for @array; # 5. for (my $i=0; $i<@array; $i++) { ... } # 6. my $i=0; while ($i<@array) { ... $i++ } # 7. my $i=0; while ($i<@array) { ... } continue { $i++ } # 9. my $i=0; ... , $i++ while $i<@array; # 10. my $i=0; MARK: ... $i++<@array and goto MARK; # 11. my $i=0; { ... $i++<@array and redo; } # 12. ... foreach 0..$#array; # 13. foreach (0..$#array) { ... } # 14. Blame [tye] for( pipe(IN,OUT) && do { local $,=$/; print IN @array,'' } && <OUT> ) + { ... }
That makes thirteen so far.(!) And I bet as I ride the train home a few more will come to me too. (I will update this node with any contributions that are made so please send em in. Id like to see if we can hit 20. :-)

So at least in this regard I think I would have to disagree with your claim.

Incidentally, I think that this is an interesting juxtapositioning (maybe the wrong word ill grant) of two key perl concepts. The first is "everything that is different should probably look different if at all possible", and the second is that "for everything that you can do there should be a few different looking variants available (TMTOWTDI)", which combine together nicely to match another generally held but certainly perlish imperitive which is that "code should look like it does what it does". So if we need to transform every element of a list to create a new one we use a different construct than we do if we want to apply a simple function to every element of the original, which is different from when we want to do something complicated with every element, and is also different from when we want to use the index, yada yada yada...

All in all i think this is great once you become used to it. When I see

my @array; $_++ foreach @count;
I instictively understand that the intention of the other programmer was very different to
my @array=map $_+1,@count;
which I find very useful in dealing with old code.

Cheers,

--- demerphq
my friends call me, usually because I'm late....


In reply to Re: Re: Self-improvement and TMTOWTDI by demerphq
in thread Self-improvement and TMTOWTDI by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.