Processing array elements without iterative loop

  • Thread starter John Markos O'Neill
  • Start date
J

John Markos O'Neill

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
 
J

Jürgen Exner

John said:
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
 
A

A. Sinan Unur

(e-mail address removed) (John Markos O'Neill) wrote in
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);
}
 
U

Uri Guttman

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
 
J

John Markos O'Neill

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
 
A

Anno Siegel

John Markos O'Neill said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top