Think of splicing as 'cut and paste' for arrays.
@foo = splice @bar, offset, length, LIST
This does the following:
- Starting with $bar[offset], 'cuts' length elements from @bar.
- 'Pastes' the LIST (this may do nothing if LIST is empty) into @bar at that very point.
- Returns the items 'cut' in step 1 (and assigning them to @foo)
I looked at
http://perldoc.com/perl5.8.0/pod/func/splice.html, and I admit that it's in need of short, simple examples, so here are some:
@bar = 0..9;
@foo = splice(@bar, 3, 2);
# $bar[3] and $bar[4] are removed.
# now @foo is (3, 4) and @bar is (0,1,2,5,6,7,8,9)
@bar = 0..9;
splice(@bar, 5, 0, 100, 200);
# Note that the 'length' is zero. This means *nothing* is removed from
+ @bar.
# then, 100 and 200 are pasted in so
# @bar is (0,1,2,3,4,100,200,5,6,7,8,9)
@bar = 0..10;
@foo = splice(@bar, 7, 3, 300);
# $bar[7], $bar[8], and $bar[9] are removed, and returned
# to @foo. the value '300' is inserted there.
# now @bar = (0,1,2,3,4,5,6,300,10)
# and @foo = (7,8,9)
Hope this helps!
--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=>
.q>.<4-KI<l|2$<6%s!<qn#F<>;$,
.=pack'N*',"@{[unpack'C*',$_]
}"for split/</;$_=$,,y[A-Z a-z]
{}cd;print lc
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.