# Array Question

Discussion in 'Perl Misc' started by amerar@iwc.net, Sep 10, 2007.

1. ### Guest

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!

, Sep 10, 2007

2. ### Benoit LefebvreGuest

On Sep 10, 10:38 am, "" <> wrote:
> 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

Benoit Lefebvre, Sep 10, 2007

3. ### Paul LalliGuest

On Sep 10, 10:38 am, "" <> wrote:
> 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

Paul Lalli, Sep 10, 2007
4. ### Paul LalliGuest

On Sep 10, 10:48 am, Benoit Lefebvre <>
wrote:
> On Sep 10, 10:38 am, "" <> wrote:
> > 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
[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

Paul Lalli, Sep 10, 2007
5. ### Guest

On Sep 10, 9:51 am, Paul Lalli <> wrote:
> On Sep 10, 10:38 am, "" <> wrote:
>
> > 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];

, Sep 10, 2007
6. ### Benoit LefebvreGuest

On Sep 10, 10:53 am, Paul Lalli <> wrote:
> On Sep 10, 10:48 am, Benoit Lefebvre <>
> wrote:
>
>
>
> > On Sep 10, 10:38 am, "" <> wrote:
> > > 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
> [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

Benoit Lefebvre, Sep 10, 2007
7. ### Benoit LefebvreGuest

On Sep 10, 10:55 am, "" <> wrote:
> On Sep 10, 9:51 am, Paul Lalli <> wrote:
>
>
>
> > On Sep 10, 10:38 am, "" <> wrote:

>
> > > 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];
> .
> .
> .
>
> }

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

Benoit Lefebvre, Sep 10, 2007
8. ### Paul LalliGuest

On Sep 10, 10:55 am, "" <> wrote:
> On Sep 10, 9:51 am, Paul Lalli <> wrote:
> > You need to read up on creating multidimensional structures in
> > Perl:
> > perldoc perlreftut
> > perldoc perllol

> 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

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

Paul Lalli, Sep 10, 2007
9. ### Guest

On Sep 10, 10:21 am, Paul Lalli <> wrote:
> On Sep 10, 10:55 am, "" <> wrote:
>
>
>
> > On Sep 10, 9:51 am, Paul Lalli <> wrote:
> > > You need to read up on creating multidimensional structures in
> > > Perl:
> > > perldoc perlreftut
> > > perldoc perllol

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

, Sep 10, 2007
10. ### Uri GuttmanGuest

>>>>> "BL" == Benoit Lefebvre <> writes:

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

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

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Uri Guttman, Sep 10, 2007
11. ### Paul LalliGuest

On Sep 10, 11:24 am, "" <> wrote:
>
> 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
to read bits and pieces of the docuements to you for you.

perldoc perlreftut
perldoc perllol

Paul Lalli

Paul Lalli, Sep 10, 2007
12. ### Guest

On Sep 10, 11:10 am, Paul Lalli <> wrote:
> On Sep 10, 11:24 am, "" <> wrote:
>
>
>
> > 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

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.

, Sep 10, 2007