Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: Splicing Arrays on pre-defined indices

by harishnuti (Beadle)
on Jun 28, 2008 at 02:31 UTC ( #694482=note: print w/replies, xml ) Need Help??

in reply to Re: Splicing Arrays on pre-defined indices
in thread Splicing Arrays on pre-defined indices

Thanks a lot for it.. but indeed the above is otherway meaning we are considering indices on what we want rather exclusion , but my requirement is we should exclude certain col by specifying indices( for ex: here @incides)..
my $line = "1!2!3!4!5!6!7!8!9"; # Original line read my @arr = split /!/, $line; my @indices = (2,5..8); #i need these indices to be sliced print join(" : ", @arr);#original splice (@arr,@indices,1); # something like this iam trying to achieve print "\n"; # i might be having 30 to 40 col of which i dont need 4 to 5, so its b +etter i slice 4 to 5 col's instead of considering 30 col's

Replies are listed 'Best First'.
Re^3: Splicing Arrays on pre-defined indices
by ikegami (Pope) on Jun 28, 2008 at 14:29 UTC
    my @exclude = (2,5..8); my @exclude_lkup; $exclude_lkup[$_] = 1 for @exclude; my $line = "1!2!3!4!5!6!7!8!9"; my @arr = split /!/, $line; my @filtered = map $arr[$_], grep !$exclude_lkup[$_], 0..$#arr; print("@filtered\n"); # 1 2 4 5
      This is awesome, this is what iam looking for....Thx a lot.
      Since I just updated my previous suggestion I figured I would Benchmark things...
      Not that it matters but I thought you might be interested.
      Update There was an issue with how I ran the test before. Here are the updated code and results.
      #!/usr/bin/perl -w use strict; use Benchmark; my $count = 500000; my $line = "1!2!3!4!5!6!7!8!9"; my @arr = split /!/, $line; my @exclude = (2,5..8); my @exclude_lkup; $exclude_lkup[$_] = 1 for @exclude; sub ikegami { my @tarr = @arr; my @filtered = map $tarr[$_], grep !$exclude_lkup[$_], 0..$#tarr; } sub injun { my @tarr = @arr; @tarr[@exclude] = (); @tarr = grep $_, @tarr; } timethese ( $count, {'Ikegami' => '&ikegami', 'InjunJoel' => '&injun'} );
      Results in
      Benchmark: timing 500000 iterations of Ikegami, InjunJoel... Ikegami: 16 wallclock secs (15.88 usr + 0.00 sys = 15.88 CPU) @ 314 +94.08/s (n=500000) InjunJoel: 10 wallclock secs ( 11.05 usr + 0.00 sys = 11.05 CPU) @ 4 +5265.25/s (n=500000)
      Though I'm not versed enough in O(n) notation to tell you why...

      "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://694482]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-10-31 08:21 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (287 votes). Check out past polls.