my $pos;
for (0..$#array) {
$pos=$_ and last
if $array[$_] eq $wanted;
}
@array[$pos,-1]=@array[-1,$pos];
And what if no one of them matched? (Left as an exercise to the reader)
Update: it was a wonderfully neat solution (which is the reason why I posted it!) to another problem!!
Not just as neat:
my $pos;
for (0..$#array) {
$pos=$_ and last
if $array[$_] eq $wanted;
}
@array=@array[0..$pos-1, $pos+1..$#array, $pos];
And what if no one of them matched? (Left as an exercise to the reader ;-)
But then if you are sure in advance that one and only one element will match, here's a neat concise solution:
@array=((grep $_ ne $wanted, @array), $wanted);
You can patch it so that it works also if the above conditions are not verified, but won't be just as elegant. (Also left as an exercise to the reader!)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
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.
|
|