in reply to array manipulation

Hi biohisham, With the splice operator, you can add items to the middle of an array or remove them, letting the array grow or shrink as needed. The portion that is removed starts at the OFFSET element of the array and continues for LENGTH elements. If the LENGTH is not specified, it will cut to the end of the array. @LIST = splice(@ARRAY, OFFSET, LENGTH, @REPLACE_WITH); Eg: Cuttinmg out some elements of one array into another @myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew'); @someNames = splice(@myNames, 1, 3); Think of the @myNames array as a row of numbered boxes, going from left to right, numbered starting with a zero. The splice() function would cut a chunk out of the @myNames array starting with the element in the #1 position (in this case, Michael) and ending 3 elements later at Matthew. The value of @someNames then becomes ('Michael', 'Joshua', 'Matthew'), and @myNames is shortened to ('Jacob', 'Ethan', 'Andrew'). Replacing some elements of an array with other elements: As an option, you can replace the portion removed with another array by passing it in the REPLACE_WITH argument. @myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew'); @moreName = ('Daniel', 'William', 'Joseph'); @someNames = splice(@myNames, 1, 3, @moreName); In the above example, the splice() function would cut a chunk out of the @myNames array starting with the element in the #1 position (in this case, Michael and ending 3 elements later at Matthew. It then replaces those names with the contents of the @moreNames array. The value of @someNames then becomes ('Michael', 'Joshua', 'Matthew'), and @myNames is changed to ('Jacob', 'Daniel', 'William', 'Joseph', 'Ethan', 'Andrew'). Gyatso

Replies are listed 'Best First'.
Re^2: array manipulation
by biohisham (Priest) on Jun 16, 2009 at 14:09 UTC
    AHA, that means this replacement is actually mutual, the second array, @someNames gives to @myNames 3 elements and takes from @myNames 3 elements or whatever the size is for that matter. the offset is zero based.... and a combination of OFFSET, LENGTH, REPLACEMENT_ARRAY(or LIST) is so powerful Thanks Gyatso
      oops...no, I meant the other way around, this replacement is not mutual,.. that is what I meant to say.. since we have three arrays involved in there