For no particular reason other than a lazy desire to type less, I want to design a routine that modifies an array in place. The following code is close, but no cigar!!
#!/usr/bin/perl -w
# test.pl --
use strict;
use warnings;
use diagnostics;
my @a = (
'one',
'two',
'three'
);
print join("\n",@a),"\n__________\n";
setmode('add_test',@a);
print join("\n",@a);
sub setmode {
my $val = shift(@_);
my @a = @_;
splice(@a,1,0,$val);
$_[$_] = $a[$_] for (0..@a);
}
Which produces:
C:>test
one
two
three
__________
one
add_test
two
Obviously @_ is an array of aliases and that portion of the direct hands on works as expected. And I've no expectation that there would be anyway to effect what I want short of references. But since TIMTOWTDI, I thought I'd ask if anyone knew a method that would allow me to have my cake and eat it too. Clearly this would work:
sub setmode {
my($val,$ref) = @_;
splice(@$ref,1,0,$val);
}
but I was looking for a little magic here...
--hsm
"Never try to teach a pig to sing...it wastes your time and it annoys the pig."
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.