Processing array elements without iterative loop

Discussion in 'Perl Misc' started by John Markos O'Neill, Dec 7, 2003.

  1. Yesterday someone suggested to me that it was possible to do something
    to each element in an array without an iterative (say, a foreach)
    loop. Well, actually, they said it could be done in one line but I
    assume they didn't just mean it would be done with an aesthetically
    displeasing removal of white space.

    In my experience, the least-lines-of-perl way to, say, print out all
    of the even elements in an array would be something like

    @array = (1, 2, 3, 4, 5, 6, 7, 8);
    foreach $array_ele (@array) {
    ($array_ele % 2 == 0) &&
    ($even_array_ele = $array_ele) &&
    print("\$even_array_ele: $even_array_ele\n");
    }

    For the sake of this discussion, let's call the three-line statement
    in the foreach loop a single line. Is it possible to do this with
    fewer statements? If so, how?

    Thanks,

    John Markos O'Neill
     
    John Markos O'Neill, Dec 7, 2003
    #1
    1. Advertising

  2. John Markos O'Neill wrote:
    > In my experience, the least-lines-of-perl way to, say, print out all
    > of the even elements in an array would be something like


    > @array = (1, 2, 3, 4, 5, 6, 7, 8);
    > foreach $array_ele (@array) {
    > ($array_ele % 2 == 0) &&
    > ($even_array_ele = $array_ele) &&
    > print("\$even_array_ele: $even_array_ele\n");
    > }
    >
    > For the sake of this discussion, let's call the three-line statement
    > in the foreach loop a single line. Is it possible to do this with
    > fewer statements? If so, how?


    What about a simple
    my @array = (1, 2, 3, 4, 5, 6, 7, 8, 13, 14);
    print grep(($_ +1) % 2, @array);

    This will print
    246814
    i.e. all the even elements in the array (just not separated by a blank, but
    you could easily insert that).

    jue
     
    Jürgen Exner, Dec 7, 2003
    #2
    1. Advertising

  3. (John Markos O'Neill) wrote in
    news::

    > In my experience, the least-lines-of-perl way to, say, print out all
    > of the even elements in an array would be something like
    >
    > @array = (1, 2, 3, 4, 5, 6, 7, 8);
    > foreach $array_ele (@array) {
    > ($array_ele % 2 == 0) &&
    > ($even_array_ele = $array_ele) &&
    > print("\$even_array_ele: $even_array_ele\n");
    > }
    >
    > For the sake of this discussion, let's call the three-line statement
    > in the foreach loop a single line. Is it possible to do this with
    > fewer statements? If so, how?


    map {print "\$even: $_\n"} grep {!($_ % 2)} (1, 2, 3, 4, 5, 6, 7, 8);

    And the interative version can be written as:

    for (@array) {
    print "$_ is even\n" unless ($_ % 2);
    }



    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Dec 7, 2003
    #3
  4. John Markos O'Neill

    Uri Guttman Guest

    >>>>> "JMO" == John Markos O'Neill <> writes:

    JMO> Yesterday someone suggested to me that it was possible to do something
    JMO> to each element in an array without an iterative (say, a foreach)
    JMO> loop. Well, actually, they said it could be done in one line but I
    JMO> assume they didn't just mean it would be done with an aesthetically
    JMO> displeasing removal of white space.

    JMO> In my experience, the least-lines-of-perl way to, say, print out all
    JMO> of the even elements in an array would be something like

    JMO> @array = (1, 2, 3, 4, 5, 6, 7, 8);
    JMO> foreach $array_ele (@array) {
    JMO> ($array_ele % 2 == 0) &&
    JMO> ($even_array_ele = $array_ele) &&
    ^^^^^^^^^^^^^^^
    why do you need that temp var? and what if the element was 0 - that
    would fail.

    JMO> print("\$even_array_ele: $even_array_ele\n");
    JMO> }

    others have shown you clean short ways. i just want to know why yours
    was much longer than it needed to be.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 7, 2003
    #4
  5. Hi all, Uri Guttman wrote,

    > why do you need that temp var? and what if the element was 0 - that
    > would fail.
    >
    > JMO> print("\$even_array_ele: $even_array_ele\n");
    > JMO> }


    I agree: the temporary variable is completely unnecessary (in fact,
    it's causing a problem) and I have eliminated it.

    > others have shown you clean short ways. i just want to know why yours
    > was much longer than it needed to be.


    I simply didn't imagine a cleaner solution. Thanks Jürgen Exner and
    A. Sinan Unur for your more elegant ones.

    John Markos O'Neill
     
    John Markos O'Neill, Dec 7, 2003
    #5
  6. John Markos O'Neill

    Anno Siegel Guest

    John Markos O'Neill <> wrote in comp.lang.perl.misc:
    > Yesterday someone suggested to me that it was possible to do something
    > to each element in an array without an iterative (say, a foreach)
    > loop. Well, actually, they said it could be done in one line but I
    > assume they didn't just mean it would be done with an aesthetically
    > displeasing removal of white space.
    >
    > In my experience, the least-lines-of-perl way to, say, print out all
    > of the even elements in an array would be something like
    >
    > @array = (1, 2, 3, 4, 5, 6, 7, 8);
    > foreach $array_ele (@array) {
    > ($array_ele % 2 == 0) &&
    > ($even_array_ele = $array_ele) &&
    > print("\$even_array_ele: $even_array_ele\n");
    > }
    >
    > For the sake of this discussion, let's call the three-line statement
    > in the foreach loop a single line. Is it possible to do this with
    > fewer statements? If so, how?


    print "@array[ map 2*$_, 0 .. $#array/2]\n"

    Anno
     
    Anno Siegel, Dec 8, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tuurbo46

    iterative structue

    Tuurbo46, Oct 5, 2005, in forum: Java
    Replies:
    0
    Views:
    633
    Tuurbo46
    Oct 5, 2005
  2. Booser
    Replies:
    1
    Views:
    1,412
    CBFalconer
    Feb 9, 2004
  3. Anthony Calabria

    Iterative numerical optimizaiton routines?

    Anthony Calabria, Oct 27, 2003, in forum: Python
    Replies:
    7
    Views:
    441
    Robert Kern
    Oct 29, 2003
  4. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
  5. Replies:
    11
    Views:
    149
    Mark Lawrence
    Jan 27, 2014
Loading...

Share This Page