Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Progresively splicing an array

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
if I have an array like this:
@data = one two three four

and I have an array of numbers which are reference numbers to the items in the first array which I want to delete (e.g. @num = 0, 2 to leave two & four in the first array), I obviously have problems doing a foreach loop:

foreach $ref (@num){
splice(@data, $ref, 1);
}

because the reference numbers of the first array change as I'm splicing them.

Does anyone know how to get round this?
 
reverse sort your array of reference numbers so that as you delete elements from the main array the references to later elements won't change.

@splice_order = reverse sort(@ref_numbers);
# then,
foreach $ref (@splice_order) { splice(@data, $ref, 1); }

I think that will do what you want.

HTH


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top