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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.