Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Removing items from an Array Reference in Place

by theleftsock (Beadle)
on Jan 09, 2013 at 16:29 UTC ( [id://1012525]=perlquestion: print w/replies, xml ) Need Help??

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

Good Morning! I searched around and I didn't find much on it. I have 2 array references and I am searching one for combinations of keys that match the other one. The inner one is the small one. When I find one, I would like to remove it from the inner array to reduce the total search space on the next iteration. My example shows the algorithm, but I didn't post the loading of the arefs. Any recommendations on a different algorithm? Any recommendations on how to modify that array? I saw in the Perl Docs that delete will be deprecated.

any suggestions would be good.

thanks -theleftsock

use warnings; use strict; my ($outer_hr_aref, $inner_hr_aref); foreach my $outer_href (@$outer_hr_aref) { foreach my $inner_href (@$inner_hr_aref) { if (($$outer_href{key1} eq $$inner_href{key2}) & ($$outer_href +{key3} == $$inner_href{key4})) { #abitrary key assignment could be ot +her combinations print "status found: $$inner_href matching $outer_href\n"; $$outer_href{key1} = 'something_new'; #arbitrary, other fu +nction calls can occur #somehow now remove the $inner_href item from the @$inner_ +hr_aref so i don't have to search the entire aref again. last; } } }

Replies are listed 'Best First'.
Re: Removing items from an Array Reference in Place
by tobyink (Canon) on Jan 09, 2013 at 16:52 UTC
    use strict; use warnings; my $arrayref = ['a', 'b', 'c', 'd', 'e', 'f']; splice(@$arrayref, 1, 2); # remove 2 items, starting at index 1 print "@$arrayref\n"; my @replacements = ('x'); splice(@$arrayref, 1, 1, @replacements); # replace 1 item, staring at +index 1 print "@$arrayref\n"; my @insert = ('l'); splice(@$arrayref, 3, 0, @insert); # replace 0 items (i.e. doing an in +sert), staring at index 3 print "@$arrayref\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      After searching more carefully. I found some ways to do this posted to perlmonks. I used the undef method posted by BrowserUk using grep. This thread perlmonks remove array items node Thanks for the help! -theleftsock

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found