Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I parse an array to grab every even numbered element?

Should I use grep?

Thanks!

Replies are listed 'Best First'.
Re: Array Parse
by Aristotle (Chancellor) on Jan 19, 2003 at 19:27 UTC
    Two solutions come to mind. Either grep as you mentioned:
    my $i = 0; my @even = grep $i++ % 2, @array
    Or you can build a list containing even numbers and use it to take a slice from the array:
    my @idx = map $_ * 2, 0 .. $#array / 2; my @even = @array[@idx];
    Here I make a list of consecutive numbers going from 0 to half the value of the biggest array index, then use map to multiply them all by two, giving me a list of all the even indices into the array. Of course you can get rid of the temporary array entirely:
    my @even = @array[map $_ * 2, 0 .. $#array / 2];
    Since both approaches loop - either over the array or over a list of indices -, I'd probably use the grep solution as it doesn't have to build an extra list, which can consume a lot of memory for large arrays. I showed the index list technique because it is also useful in other cases - f.ex, when you want to access the same elements of several arrays, it's usually the best approach.

    Makeshifts last the longest.

      A more general application of your second technique was posted by Abigail-II in Re: Stepping through an array - This thread also includes a benchmark between these grep and slice techniques.

       

      perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011111"))'

      I would not actually recommend this (except for an obfuscation maybe) but I thought it deserved mention anyway:

      my @even = grep --$|, @array;

      -sauoq
      "My two cents aren't worth a dime.";
      
        Obfuscation indeed! What makes this tick? I though the $| had to do with pipes, plumbing and toilets! Please explain?
Re: Array Parse
by Marza (Vicar) on Jan 19, 2003 at 19:33 UTC

    We like to see at least an attempt when requesting for help.

    You would use a loop to go through the array. You can use a counter and increase its value by 2 each iteration of the loop.

    Now if you mean the contents of the array values being even then you can use the modulos operator % to check for evens.

    Ack

    Hit the submit key instead of preview....

    I was going to add some stuff about grep and map but Aristotle already did that..

      please accept my apologies for not posting my code, but I was just curious if there was an alternative to using grep.

      Thanks again for the help and advice

        No worries. When classes start, we get many "write my assingment for me" requests.

        Hey if you are serious about learning, create an account and join the group. You will learn many things here!

Re: Array Parse
by OM_Zen (Scribe) on Jan 19, 2003 at 19:41 UTC
    Hi ,

    The even numbered element , is that the even numbered index of the array or is the element an even number