### Re: Splicing Arrays on pre-defined indices

by injunjoel (Priest)
 on Jun 27, 2008 at 18:33 UTC ( #694420=note: print w/replies, xml ) Need Help??

in reply to Splicing Arrays on pre-defined indices

Here is something to try,
```my \$line = "1!2!3!4!5!6!7!8!9";
my @arr = split /!/, \$line;
my @indices = (2,5..8); #just remember indexing starts at 0.
print join(" : ", @arr);#original
print "\n";
print join(" : ", @arr[2,5..8]);#explicit
print "\n";
print join(" : ", @arr[@indices]);#predefined

Output
```1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9
3 : 6 : 7 : 8 : 9
3 : 6 : 7 : 8 : 9

Update
Just thought of another quick way to do this...
```my \$line = "1!2!3!4!5!6!7!8!9";
my @arr = split /!/, \$line;
my @exclude = (2,5..8); #just remember indexing starts at 0.

#set the elements to nothing and then filter them out.
@arr[@exclude] = ();
@arr = grep \$_, @arr;
Just another thought,

-InjunJoel
"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

Replies are listed 'Best First'.
Re^2: Splicing Arrays on pre-defined indices
by harishnuti (Beadle) on Jun 28, 2008 at 02:31 UTC
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
```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...

-InjunJoel
"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
Re^2: Splicing Arrays on pre-defined indices
by harishnuti (Beadle) on Jul 09, 2008 at 09:19 UTC

Just an update on the question i asked earlier

actually iam stuck with the below in my project , so iam putting the following piece of code from my main script
```while (<process file delimited by ~ symbol>){
@data = split(/\~/,\$_); # will have around 30 elems
# \$data[23] will have user preference column selection
# user enters 3,3-7,4,9-13 etc anything he desires
\$data[23] =~ s/-/\.\./; # convert - to .. perl syntax
# 23rd elements contains the range entered by user
# for ex: 2,3-8,11-20
# i need to extract only above range ignoring rest
push(@range,eval \$array[23]); # i expanded here
@range = map { --\$_ } @range; # decrementing since array starts
+ from 0
# @range = grep { !\$seen{\$_}++ } @range; # remove duplicates in
+ case user enters like 2,1-10 etc
# @range = sort { \$a <=> \$b } @range; # Sort thee indices in as
+cending order like 1,2,3,4,5,6 and so on
my @finalarray = @data[@range];

iam having trouble when i print final array i get all junk values, iam sure something is wrong or there can be another good approach to achieve above

you help is highly appreciated and helpful for me

sorry for not providing sample data, because file is pretty large and question is straight forward, i have spent quite sometime on above today

Create A New User
Node Status?
node history
Node Type: note [id://694420]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-08-23 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (355 votes). Check out past polls.

Notices?