how to init three dimentional array using perl 
Author Message
 how to init three dimentional array using perl

Dear expert,

how to init three dimentional array and how the syntax look like ???
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 ???

Thanks in advance.

John Vun



Mon, 13 Nov 2000 03:00:00 GMT  
 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



Mon, 13 Nov 2000 03:00:00 GMT  
 how to init three dimentional array using perl

On Thu, 28 May 1998 18:09:17 +0800,

Quote:

> 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 ????

Obviously not, right? or you wouldn't be asking.

Problem #1: Braces are mandatory in Perl, they are not optional as in C.

Problem #2: When assigning to specific scalar members of an array, use $,

Problem #3: Perl's multi-dimensional array syntax doesn't look like that.

Tip #4: Your indices are numeric, and the array is to be fully
        initialized (not sparse), so use arrays rather than hash.

Putting it all together:

for ( $in = 1; $in < 13; $in++ ) {
        for ( $shop =1; $shop < 120; $shop++ ) {
                for ( $trans=1; $trans < 100; $trans++ ) {
                        $isarray[$in][$shop][$trans] = 0;
                }
        }

Quote:
}
> how i check my array is zero ???

By this I assume you mean, "how do I check that all elements are
zero".  Here's a simple way:

$all_zero = 1;
CHECK_ZERO:
for ( $in = 1; $in < 13; $in++ ) {
        for ( $shop =1; $shop < 120; $shop++ ) {
                for ( $trans=1; $trans < 100; $trans++ ) {
                        if ( $isarray[$in][$shop][$trans] ) {
                                $all_zero = 0;
                                last CHECK_ZERO;
                        }
                }
        }

Quote:
}

if ( $all_zero ) { ...

hth,
John Porter



Mon, 13 Nov 2000 03:00:00 GMT  
 how to init three dimentional array using perl

Quote:
>> Dear expert

I'm not an expert but I'll try to help.

My best suggestion is: get yourself a Camel. Read it thoroughly. It's
all explained there.

The following transformation of your code preserves the C-ish feel, but
it's certainly not the best, nor most efficient way.

for ($in = 0; $in < 12; $in++)
{
   for ($shop =0; $shop < 119; $shop++)
   {
      for ($trans=0; $trans<99; $trans++)
      {
         $isarray->[$in][$shop][$trans] = 0;
      }
   }

Quote:
}

This makes $isarray a reference to a three-dimensional array (assuming
that $isarray was previously undefined).

Note that Perl arrays begin at index 0 (notwithstanding a
now-deprecated feature).

Jean-Louis Leroy
http://ourworld.compuserve.com/homepages/jl_leroy



Mon, 13 Nov 2000 03:00:00 GMT  
 how to init three dimentional array using perl

Quote:

> By this I assume you mean, "how do I check that all elements are
> zero".  Here's a simple way:

> $all_zero = 1;
> CHECK_ZERO:
> for ( $in = 1; $in < 13; $in++ ) {
>         for ( $shop =1; $shop < 120; $shop++ ) {
>                 for ( $trans=1; $trans < 100; $trans++ ) {
>                         if ( $isarray[$in][$shop][$trans] ) {
>                                 $all_zero = 0;
>                                 last CHECK_ZERO;
>                         }
>                 }
>         }
> }
> if ( $all_zero ) { ...

> hth,
> John Porter

Greetings:

        I have a follow up question here. Don't you need to do

        if ( exists $isarray[$in][$shop][$trans] ) {

        This always gets me. You need to check the existents of the
        element first before you access. Got this form the Advanced
        Perl Programming book. This hash of hashes is what always
        gets me. I never get it right the first, second , third.....

        Tom



Tue, 14 Nov 2000 03:00:00 GMT  
 how to init three dimentional array using perl

On Fri, 29 May 1998 08:33:46 -0400,

Quote:

>    I have a follow up question here. Don't you need to do

>    if ( exists $isarray[$in][$shop][$trans] ) {

>    This always gets me. You need to check the existents of the
>    element first before you access. Got this form the Advanced
>    Perl Programming book. This hash of hashes is what always
>    gets me. I never get it right the first, second , third.....

No, for the simple reason that 'exists' is hash-specific;
it is not meant to be used (doesn't work) for arrays.  
Second simple reason: we want to test each item for "truth",
which in our case is a non-zero value.  I could have said:

        if ( $isarray[$in][$shop][$trans] != 0 ) {

and I guess I probably should have.

John Porter



Tue, 14 Nov 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to have three dimentional array

2. Mutli-dimentional array emulation in Perl

3. Multi-dimentional arrays in perl

4. emulating mulit-dimentional arrays?

5. Grepping/Comparing two-dimentional arrays?

6. getting the last element position in multi dimentional array

7. Sorting a Multi-dimentional Array

8. 2 dimentional array

9. Q. mulit dimentional arrays

10. How to init arrays at startup

11. How do I sort a 2D-array by three different columns with perl

12. three-part form submission using CGI.pm

 

 
Powered by phpBB® Forum Software