Click to See Complete Forum and Search --> : 2-D Array in PERL?


Ultimater
03-21-2005, 04:53 PM
Hey, I'm having problems creating and refering to a 2D Array in PERL.
I'm trying to create an Array called guestbook_contents.
I want each element of guestbook_contents to be equal to each
new InfoArray Array from within my while-loop.
Thus, I'm trying to make it a 2D Array.
I'm not familar with PERL's Array format.
How do I create a 2D Array and print all the elements to the screen?


open(GUEST_LOG, $GuestBookPath) || die "Can't OPEN guest book,
please create a file called: $GuestBookPath .\n$!";
@guestbook_contents=(); # Set up empty Array
$guestbook_length=-1; # Array Element length counter
$GuestEntryStruct = "a30 a30 a30 a30 a30 a30 a256 l";
$GuestEntrySize =440; # all the sizes added up.
$NumElements =8; # Number of elements

while (read (GUEST_LOG, $buffer, $GuestEntrySize)){
@InfoArray=unpack($GuestEntryStruct, $buffer);
for($n=0;$n<($NumElements-1);$n++){$InfoArray[$n]=~s/\0//g;}
# This is where I am trying to create a 2D Array
$guestbook_length++;
$guestbook_contents[$guestbook_length]=@InfoArray;
}

close(GUEST_LOG);
# This is where I'm trying to print all the elements in the 2D Array.
# (I don't care how they're joined)
print @guestbook_contents;

Jeff Mott
03-21-2005, 07:04 PM
push(@guestbook_contents, [@InfoArray]);

...

for my $CurrInfoArrayRef (@guestbook_contents) {
print @{$CurrInfoArrayRef};
}

Ultimater
04-03-2005, 01:01 AM
Sorry Jeff Mott, I don't follow what your code does.
I'm not sure what to place in-place-of your three-dots. Does your example
actually create a 2-D array or does it just pack all the elements into a single array?

Ultimater
04-03-2005, 01:11 AM
What I'm trying to do, in my code, is obtain all the information inside of one of my files and shove it into a 2-D array.
The reason, why I want the data inside of an actual 2-D array as-a-posed-to merging
all the data into a single array, is so I can delete one of the parent-elements and include all of the child elements with it. Each parent-element corresponds to an actual
entry inside of my guest book by a user. Each child-element corresponds to all the
entry information by the user (First Name, Last Name, ect...)

Charles
04-03-2005, 08:37 AM
Perl doesn't have multi-dimensional arrays. You have to make an array of references to other arrays. Though this being Perl, the beloved language, it would be better put that you get to make an array of references to other arrays.

Jeff Mott
04-03-2005, 10:21 AM
$guestbook_length++;
$guestbook_contents[$guestbook_length]=@InfoArray;These two lines here is replaced withpush(@guestbook_contents, [@InfoArray]);The square brackets make a new annoymous array reference that holds all the items that were within @InfoArray. This array reference is then appended (pushed) to the end of the array @guestbook_contents.for my $CurrInfoArrayRef (@guestbook_contents) {
print @{$CurrInfoArrayRef};
}And this code prints it out. If you wanted to index it so it looks more like a 2D array then you could do this.for (my $i = 0; $i < @guestbook_contents; $i++) {
for (my $j = 0; $j < @{$guestbook_contents[$i]}; $j++) {
print $guestbook_contents[$i][$j];
}
}

Ultimater
04-03-2005, 04:03 PM
Hey, Charles,
can ya show me how to setup an array containing a bunch of references?
How would you translate an array like: [["FF","IE"],["NS","OP"]]
from JavaScript into PERL using references?

Charles
04-03-2005, 04:37 PM
In Perl it would be

(['FF','IE'],['NS','OP']);

Perl uses parentheses '()' to make arrays while JavaScript uses brackets '[]'. Brackets in Perl make references to arrays.

JavaScript doesn't have multi-dimensional arrays either. But it has Arrays of Arrays instead of Perl's arrays of array references. And yes, my capitalization is intentional.

Ultimater
04-03-2005, 06:08 PM
Thanks Charles
Your example worked.
Here's how I tested it:

#!/usr/local/bin/perl

@MyArray=(['FF','IE'],['NS','OP']);
print "Content-type: text/html\n\n";
print $MyArray[0][0],", ";
print $MyArray[0][1],"<br>";
print $MyArray[1][0],", ";
print $MyArray[1][1];


So, what do you mean by "Perl doesn't have multi-dimensional arrays" and "JavaScript doesn't have multi-dimensional arrays either" ?
What would you call a multi-dimensional array?

Charles
04-03-2005, 09:00 PM
If JavaScript had multi-dimensional Arrays then the following would work:

a = new Array ();
a[1][1] = 'foo';
document.write (a[1][1]);

It doesn't, but we can do:

a = new Array();
a[0] = 'foo';
a[1] = ['fee', 'fie', 'foe', 'fum'];
a[3] = document.write;
a[3](a[1][1]);

We have single dimensional Arrays and each member can be any other data type.

The following works in Perl:

my @a = ();
$a[1][1] = 'foo';
print $a[1][1];

But this doesn't:

my @a = ();
my @b = qw(fee fie foe fum);
$a[1] = @b;
print $a[1];

because each element of a Perl array must be a scalar. Array's aren't scalar but references to them are. Thus:

my @a = ();
my @b = qw(fee fie foe fum);
$a[1] = \@b;
print @{$a[1]};

or

my @a = ();
my @b = qw(fee fie foe fum);
$a[1] = \@b;
print $a[1][1];

Ultimater
04-03-2005, 10:50 PM
Oh, I see. So do you know of any languages that have such a flexibility?