Create or Put Items into an Array in Perl
To create or put items into an
array in Perl:
@jiji=("Hi","Hello","Hiya");
print @jiji;
Which prints:
HiHelloHiya
-We named our array
jiji because it's fun to type
jiji. (Arrays start with @ unlike scalars which start with $)
-We stored our three items, separated by commas with each enclosed in quotation marks.
-We
printed our array.
NOTES: Even though we created this array in a very formal manner. Almost anything can be converted into an array and pieced out using Perl's built-in
split function. Just slap an @ in front of something!
Imagine you have a scalar variable
$ko...
$ko="England wins World Cup.";
@jiji=split(/ /, $ko);
print $jiji[1];
Which Prints:
wins
-We created a variable named
ko and stored some words there.
-We used Perl's
split function on our variable
$ko and stored the pieces into our new array
jiji. (We split at the spaces / /)
-We printed the second piece of @jiji. (
Index numbers start at 0, so $jiji[0] would equal
England.)
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
@jiji=("Hi<br>","Hello<br>","Hiya<br>");
print @jiji;
$ko="England wins World Cup.";
@jiji=split(/ /, $ko);
print $jiji[1];
exit;
Resources:
Perl:
Use the Perl PRINT FUNCTION
Splitting an ARRAY in Perl
Undefine an Array in Perl
What's in an Array, Index Numbers
Finding the Length (Index Numbers) of an Array in Perl
Javascript:
Splitting an Array using Javascript
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | | CSS David McFarland |
| | | |
| | | |