Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How can I skip an element in an array whilst reading...

by Blanco (Initiate)
on Apr 05, 2002 at 23:49 UTC ( #157082=perlquestion: print w/replies, xml ) Need Help??

Blanco has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

How can I skip an element in an array whilst reading data from each 'side' of the array into two seperate arrays, eg:

input from an array already pre-defined:

2022, 2002
      6, 78
1023, 1232

I would like two arrays like so:
array 1:
2022
6
1023

array 2:
2002
78
1232

The array will be very large and may change in length.
Cheers

Originally posted as a Categorized Question.

  • Comment on How can I skip an element in an array whilst reading...

Replies are listed 'Best First'.
Re: How can I skip an element in an array whilst reading...
by tachyon (Chancellor) on Apr 06, 2002 at 01:58 UTC

    For a non-destructive method that leaves the original array intact (we use a modulus flip-flop):

    for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; }
Re: How can I skip an element in an array whilst reading...
by belg4mit (Prior) on Apr 06, 2002 at 00:16 UTC
    I fail to see where the "either side" comes from.... If you mean your array is:

    2022, 2002, 6, 78 1023, 1232

    Then the following should work:

    while( @in ){ push @out1, shift(@in); push @out2, shift(@in); }
Re: How can I skip an element in an array whilst reading...
by tachyon (Chancellor) on Apr 07, 2002 at 10:17 UTC

    This C style approach is 50% faster than the push approach posted earlier. Ugly but fast.

    use Benchmark; @in = (1,2,3,4,5,6,7,8,9,0); timethese ( 100000, { 'C-style' => 'for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $i +n[$i] }', 'push' => 'for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; }' } ); __DATA__ Benchmark: timing 100000 iterations of C-style, push... C-style: 13 wallclock secs (12.97 usr + 0.00 sys = 12.97 CPU) @ 77 +10.10/s (n=100000) push: 18 wallclock secs (18.79 usr + 0.00 sys = 18.79 CPU) @ 53 +21.98/s (n=100000)
      Funny, I get
      q( Benchmark: timing 100000 iterations of C-style, push... C-style: 2 wallclock secs ( 3.20 usr + 0.00 sys = 3.20 CPU) @ 31 +279.32/s (n=100000) push: 3 wallclock secs ( 2.63 usr + 0.09 sys = 2.72 CPU) @ 36 +724.20/s (n=100000)
      I even went so far as to gather:
      #These "cripple" C-style and push to make it a fair fight, by reinitia +lizing the array each time Benchmark: timing 100000 iterations of C-style, destroy, push... C-style: 4 wallclock secs ( 3.88 usr + 0.00 sys = 3.88 CPU) @ 25 +779.84/s (n=100000) destroy: 2 wallclock secs ( 2.25 usr + 0.08 sys = 2.33 CPU) @ 42 +918.45/s (n=100000) push: 4 wallclock secs ( 3.24 usr + 0.07 sys = 3.31 CPU) @ 30 +184.12/s (n=100000) #These do not: Benchmark: timing 1000000 iterations of C-style, destroy, push... C-style: 34 wallclock secs (32.64 usr + 0.00 sys = 32.64 CPU) @ 30 +633.50/s (n=1000000) destroy: 24 wallclock secs (22.72 usr + 0.81 sys = 23.53 CPU) @ 42 +498.94/s (n=1000000) push: 2 wallclock secs ( 2.38 usr + 0.00 sys = 2.38 CPU) @ 41 +9463.09/s(n=1000000) );
      @in = (1,2,3,4,5,6,7,8,9,0); use Benchmark; timethese(100000, { 'C-style' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $in[$ +i]; }', 'push' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; }', 'destroy' => '@in = (1,2,3,4,5,6,7,8,9,0); while( @in ){ push @out1, shift(@in); push @out2, shift(@in); }' });
      Statistics can say lots of things
      q( #These "cripple" C-style and push to make it a fair fight, by reinitia +lizing the array each time C-style: 1 wallclock secs ( 0.57 usr + 0.04 sys = 0.60 CPU) +@ 16528.93/s (n=10000) destroy: 337 wallclock secs (325.85 usr + 0.28 sys = 326.13 CPU) +@ 30.66/s (n=10000) push: 1033 wallclock secs (983.44 usr + 0.85 sys = 984.30 CPU) +@ 10.16/s (n=10000) #These do not Benchmark: timing 10000 iterations of C-style, destroy, push... C-style: 1 wallclock secs ( 0.47 usr + 0.03 sys = 0.51 CPU) @ + 19762.85/s (n=10000) destroy: 339 wallclock secs (321.60 usr + 0.32 sys = 321.92 CPU) @ + 31.06/s (n=10000) push: 655 wallclock secs (641.02 usr + 0.54 sys = 641.56 CPU) @ + 15.59/s (n=10000) ); open OUT, ">/dev/null"; select(OUT); $|++; select(STDOUT); @in = (1,2,3,4,5,6,7,8,9,0); use Benchmark; timethese(10000, { 'C-style' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? $out2[int($i/2)] = $in[$i] : $out1[$i/2] = $in[$ +i]; } print OUT @out1,"\n"; print OUT @out2,"\n";', 'push' => '#@in = (1,2,3,4,5,6,7,8,9,0); for my $i ( 0 .. $#in ) { $i % 2 ? push @out2, $in[$i] : push @out1, $in[$i]; } print OUT @out1,"\n"; print OUT @out2,"\n";', 'destroy' => '@in = (1,2,3,4,5,6,7,8,9,0); while( @in ){ push @out1, shift(@in); push @out2, shift(@in); } print OUT @out1,"\n"; print OUT @out2,"\n";' });

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: How can I skip an element in an array whilst reading...
by perlplexer (Hermit) on Apr 06, 2002 at 19:06 UTC
    $|-- ? push @a, $_ : push @b, $_ for @in;
      Tsk tsk tsk, you could potentially leave $| in a state other than which you began ;-) That also relies upon it being unset initially in order for the first element of @in to end up @b

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: How can I skip an element in an array whilst reading...
by Russ (Deacon) on Apr 06, 2002 at 00:18 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://157082]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-06-26 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (84 votes). Check out past polls.

    Notices?