You could use references to make your code easier in many ways. You may know this:
sub ShowIt {
print join(',',@_);
}
@Foo = (1,2,3);
&ShowIt(@Foo);
Pretty easy, isn't it? But if we wan't to give two arrays, we got a problem - this isn't possible. Here we could use references:
sub ShowIt {
my $Array1 = $_[0];
my $Array2 = $_[1];
print "Array 1: ".join(',',@$Array1)."\n";
print "Array 2: ".join(',',@$Array2)."\n";
}
@Foo = (1,2,3);
@Bar = (4,5,6);
&ShowIt(\@Foo,\@Bar);
Here we go:
Foo and Bar are Arrays which are defined as usual.
ShowIt is called and the two Arrays are passed, but if you would pass them directly, there would be no chance to seperate which value was from which Array. They are passed as references which means: "Use the Array found at the memory location x" and references are indicated by prefixing the thing (scalar, array or hash) by a \.
ShowIt now puts each argument into a variable (not necessary, just for visibility)
The JOINs dereference (= follow the reference) the arguments. They say: Interpret the content of $Array1 as a @ (Array)
References make many things easier, just think of:
@Cities_DE = ('Berlin','Hannover','Hamburg');
@Cities_US = ('New York','Miami','Las Vegas');
%Cities = ('DE' => \@Cities_DE,
'US' => \@Cities_US);
print "Where are you?".join(',',@$Cities{$Country});
You could also create references to so-called anonymus Scalars, Arrays and Hashs, but I'll leave this to the Perldoc-document...
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.