Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Processing array elements without iterative loop

Reply
Thread Tools

Processing array elements without iterative loop

 
 
John Markos O'Neill
Guest
Posts: n/a
 
      12-07-2003
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, ;
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-07-2003
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, ;
> 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


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-07-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (John Markos O'Neill) wrote in
news:(E-Mail Removed) om:

> 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, ;
> 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, ;

And the interative version can be written as:

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



--
A. Sinan Unur
(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-07-2003
>>>>> "JMO" == John Markos O'Neill <(E-Mail Removed)> 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, ;
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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
John Markos O'Neill
Guest
Posts: n/a
 
      12-07-2003
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-08-2003
John Markos O'Neill <(E-Mail Removed)> 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, ;
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
DNS Question: Recursive vs. Iterative SWE MCSE 20 04-08-2011 06:29 PM
iterative structue Tuurbo46 Java 0 10-05-2005 10:43 AM
Iterative Merge Sort Using Circular Linked List Booser C Programming 1 02-09-2004 07:12 PM
Iterative numerical optimizaiton routines? Anthony Calabria Python 7 10-29-2003 09:52 PM



Advertisments