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. Advertisements

  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. Advertisements

  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. Advertisements

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. P
    Replies:
    1
    Views:
    1,401
    Joe Kesselman
    Jul 7, 2006
  2. defn noob
    Replies:
    4
    Views:
    739
    Carl Banks
    Jun 29, 2008
  3. Bernd Burnt
    Replies:
    7
    Views:
    222
    Robert Dober
    Jul 13, 2007
  4. Saeed Bhuta
    Replies:
    5
    Views:
    368
    Charles Johnson
    Apr 22, 2010
  5. antsor
    Replies:
    0
    Views:
    966
    antsor
    Feb 12, 2012
  6. Brandon Hoppe

    for loop is not going to all array elements

    Brandon Hoppe, Mar 30, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    287
    Anno Siegel
    Mar 30, 2006
  7. Isaac Won
    Replies:
    9
    Views:
    798
    Ulrich Eckhardt
    Mar 4, 2013
  8. Replies:
    11
    Views:
    304
    Mark Lawrence
    Jan 27, 2014
Loading...