Array Question

A

amerar

Hi,

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:

$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9

Basically, I have 1 row and 3 columns.........how would I store the values,
and retrieve the values???

Thanks!
 
B

Benoit Lefebvre

Hi,

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:

$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9

Basically, I have 1 row and 3 columns.........how would I store the values,
and retrieve the values???

Thanks!

You can make an array of array

$x[1][1] = 1;
$x[1][2] = 2;
$x[1][3] = 3;
$x[2][1] = 4;
....

see:
http://www.unix.org.ua/orelly/perl/prog3/ch09_01.htm

--Ben
 
P

Paul Lalli

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:

$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9

my @x = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );

OR

my @x;
$x[0] = [ 1, 2, 3 ];
$x[1] = [ 4, 5, 6 ];
$x[2] = [ 7, 8, 9 ];
Basically, I have 1 row and 3 columns.........how would I store
the values, and retrieve the values???

print "(0,2) = $x[0][2]\n";

You need to read up on creating multidimensional structures in Perl:

perldoc perlreftut
perldoc perllol

Paul Lalli
 
P

Paul Lalli

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:
$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9
Basically, I have 1 row and 3 columns.........how would I store
the values, and retrieve the values???
You can make an array of array

$x[1][1] = 1;
$x[1][2] = 2;
$x[1][3] = 3;
$x[2][1] = 4;

You're sticking undefined values all over the place. Arrays in Perl
start with 0, not 1. You've made $x[0] undefined, as well as $x[1]
[0], $x[2][0], etc.
see:<link to pirated material snipped.>

Please don't do that again. Perl has free built-in documentation
available. Reference that. http://perldoc.perl.org. Do not post
links to commercially available material that people have illegally
duplicated.

Paul Lalli
 
A

amerar

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:
$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9

my @x = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );

OR

my @x;
$x[0] = [ 1, 2, 3 ];
$x[1] = [ 4, 5, 6 ];
$x[2] = [ 7, 8, 9 ];
Basically, I have 1 row and 3 columns.........how would I store
the values, and retrieve the values???

print "(0,2) = $x[0][2]\n";

You need to read up on creating multidimensional structures in Perl:

perldoc perlreftut
perldoc perllol

Paul Lalli

So, if I would want to use MySQL and pull some values from a table and
store them in an array, could I use a method like this:

while (($customer_id, $report_name, $report_string) $sel-
fetchrow_array()) {
$y = 0;
$info[$x][$y] = $customer_id;
$y++
$info[$x][$y] = $report_name;
$y++
$info[$x][$y] = $report_string;
$x++;
}

And, if I wanted to use a foreach loop to process the array, can I do
something like this:

foreach $info (@info) {
$customer_id = $info[0];
$rpt_name = $info[1];
$rpt_str = $info[2];
 
B

Benoit Lefebvre

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:
$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9
Basically, I have 1 row and 3 columns.........how would I store
the values, and retrieve the values???
You can make an array of array
$x[1][1] = 1;
$x[1][2] = 2;
$x[1][3] = 3;
$x[2][1] = 4;

You're sticking undefined values all over the place. Arrays in Perl
start with 0, not 1. You've made $x[0] undefined, as well as $x[1]
[0], $x[2][0], etc.
see:<link to pirated material snipped.>

Please don't do that again. Perl has free built-in documentation
available. Reference that. http://perldoc.perl.org. Do not post
links to commercially available material that people have illegally
duplicated.

Paul Lalli

Oh, sorry for that..

I just realised what it was..

I did a search on google and found that this document was well done
and had what he was requesting for.

--Ben
 
B

Benoit Lefebvre

I need to create an array. Each element will have 1 row and 3
columns. So, the array would look something like this:
$x[1] = 1,2,3
$x[2] = 4,5,6
$x[3] = 7,8,9
my @x = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );

my @x;
$x[0] = [ 1, 2, 3 ];
$x[1] = [ 4, 5, 6 ];
$x[2] = [ 7, 8, 9 ];
Basically, I have 1 row and 3 columns.........how would I store
the values, and retrieve the values???
print "(0,2) = $x[0][2]\n";
You need to read up on creating multidimensional structures in Perl:
perldoc perlreftut
perldoc perllol
Paul Lalli

So, if I would want to use MySQL and pull some values from a table and
store them in an array, could I use a method like this:

while (($customer_id, $report_name, $report_string) $sel->fetchrow_array()) {

$y = 0;
$info[$x][$y] = $customer_id;
$y++
$info[$x][$y] = $report_name;
$y++
$info[$x][$y] = $report_string;
$x++;

}

And, if I wanted to use a foreach loop to process the array, can I do
something like this:

foreach $info (@info) {
$customer_id = $info[0];
$rpt_name = $info[1];
$rpt_str = $info[2];
.
.
.

}

For that.. maybe you can use an ash or array or ash of ashes
 
P

Paul Lalli

So, if I would want to use MySQL and pull some values from a
table and store them in an array, could I use a method like this:
while (($customer_id, $report_name, $report_string) $sel->fetchrow_array()) {

$y = 0;
$info[$x][$y] = $customer_id;
$y++
$info[$x][$y] = $report_name;
$y++
$info[$x][$y] = $report_string;
$x++;

}

I suppose you *could*, but why would you want to??
my @info;
while (my $ref = $sel->fetchrow_arrayref) {
push @info, [ @{$ref} ];
}
And, if I wanted to use a foreach loop to process the array,
can I do something like this:

foreach $info (@info) {
$customer_id = $info[0];
$rpt_name = $info[1];
$rpt_str = $info[2];

No. $info is a reference to an array. You need to dereference the
array. Did you read those two perldocs I linked you to?

my $customer_id = $info->[0];
my $rpt_name = $info->[1];
my $rpt_str = $info->[2];

OR:

my ($customer_id, $rpt_name, $rpt_str) = @{$info};

Paul Lalli
 
A

amerar

So, if I would want to use MySQL and pull some values from a
table and store them in an array, could I use a method like this:
while (($customer_id, $report_name, $report_string) $sel->fetchrow_array()) {
$y = 0;
$info[$x][$y] = $customer_id;
$y++
$info[$x][$y] = $report_name;
$y++
$info[$x][$y] = $report_string;
$x++;

I suppose you *could*, but why would you want to??
my @info;
while (my $ref = $sel->fetchrow_arrayref) {
push @info, [ @{$ref} ];

}
And, if I wanted to use a foreach loop to process the array,
can I do something like this:
foreach $info (@info) {
$customer_id = $info[0];
$rpt_name = $info[1];
$rpt_str = $info[2];



No. $info is a reference to an array. You need to dereference the
array. Did you read those two perldocs I linked you to?

my $customer_id = $info->[0];
my $rpt_name = $info->[1];
my $rpt_str = $info->[2];

OR:

my ($customer_id, $rpt_name, $rpt_str) = @{$info};

Paul Lalli

Dereference......I see how you are referring to each 'column' in the
array, but how do you access each row of the array?
 
U

Uri Guttman

BL> Oh, sorry for that..

BL> I just realised what it was..

BL> I did a search on google and found that this document was well done
BL> and had what he was requesting for.

you should be able to tell a pirated book from a free web document. most
recent computer books which have ebook formats too have pirated copies
on the web.

uri
 
P

Paul Lalli

Dereference......I see how you are referring to each 'column' in the
array, but how do you access each row of the array?- Hide quoted text -

Stop. Go read those two documents. Then if you still don't
understand something, come back and ask your question. I'm not going
to read bits and pieces of the docuements to you for you.

perldoc perlreftut
perldoc perllol

Paul Lalli
 
A

amerar

Stop. Go read those two documents. Then if you still don't
understand something, come back and ask your question. I'm not going
to read bits and pieces of the docuements to you for you.

perldoc perlreftut
perldoc perllol

Paul Lalli

Read the documents. Everything refers to dereferencing, but nothing
really gives an example on running through a loop and accessing each
row, and then accessing columns. Just a bit confusing for me. Not
asking for someone to do it, but since I do not understand, a small
example would help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top