
how to init three dimentional array using perl
: how to init three dimentional array and how the syntax look like ???
$array[1][2][3] = 'value'; # this is just one element though
: i want to init the three dimentional array be zero.
: for ($in = 1; $in < 13; $in++)
: for ($shop =1; $shop < 120; $shop++)
: for ($trans=1; $trans<100; $trans++)
: is this syntax ok ????
: how i check my array is zero ???
Woops, now quite right.
foreach my $in (1..12) {
foreach my $shop (1..119) {
foreach my $trans (1..99) {
$isarray[$in][$shop][$trans] = 0;
}
}
}
-Although I question why you're starting from 1 and not 0...
Brackets are *always* needed in perl, unlike C. Most people
consider this a Good Thing[tm]. You can use a for() loop, but
foreach () is faster and normally cleaner to read too.
When thinking about what kind of thingy identifier you need to
what the variable is but what kind of data you are assigning or
reading from it. If you're reading/writing a single (ala
scalar) value from an array or hash, you want the scalar thingy
marker:
$array[0] = 'value';
$hash{foo} = 'value';
This also applys to assigning values to hashes in an "array" style,
in which case you need the array thingy marker even though you're
talking about a hash:
## This sets the three keys of %hash at once, but has nothing
When working with arrays and lists, you are talking about indexs,
so you need the index style markers, "[]". When talking about
hashes, you need hash style markers, "{}". Don't mix them, or
you'll do something you didn't want to do. Seeing what kind of
markers a variable is using is the single big clue as to what it
really is. Anything with "[]" is going to be an array. Anything
with "{}" is going to be a hash. The exception to this is when
the array or hash is being stored as a reference, in which case
it still applys, but indirectly as the reference to the array or
hash thingy is in a scalar, which might though one off as it
would use ->[] or ->{} for access to the referenced thingys.
See, simple right? <evil grin>
--
-Zenin