Velocity Reviews > Perl > Array Question

# Array Question

amerar@iwc.net
Guest
Posts: n/a

 09-10-2007

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!

Benoit Lefebvre
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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

Paul Lalli
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:48 am, Benoit Lefebvre <benoit.lefeb...@gmail.com>
wrote:
> On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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

amerar@iwc.net
Guest
Posts: n/a

 09-10-2007
On Sep 10, 9:51 am, Paul Lalli <mri...@gmail.com> wrote:
> On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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];

Benoit Lefebvre
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:53 am, Paul Lalli <mri...@gmail.com> wrote:
> On Sep 10, 10:48 am, Benoit Lefebvre <benoit.lefeb...@gmail.com>
> wrote:
>
>
>
> > On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:55 am, "ame...@iwc.net" <ame...@iwc.net> wrote:
> On Sep 10, 9:51 am, Paul Lalli <mri...@gmail.com> wrote:
>
>
>
> > On Sep 10, 10:38 am, "ame...@iwc.net" <ame...@iwc.net> 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

Paul Lalli
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:55 am, "ame...@iwc.net" <ame...@iwc.net> wrote:
> On Sep 10, 9:51 am, Paul Lalli <mri...@gmail.com> 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

amerar@iwc.net
Guest
Posts: n/a

 09-10-2007
On Sep 10, 10:21 am, Paul Lalli <mri...@gmail.com> wrote:
> On Sep 10, 10:55 am, "ame...@iwc.net" <ame...@iwc.net> wrote:
>
>
>
> > On Sep 10, 9:51 am, Paul Lalli <mri...@gmail.com> 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?

Uri Guttman
Guest
Posts: n/a

 09-10-2007
>>>>> "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