Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array Question

Reply
Thread Tools

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!

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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
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

 
Reply With Quote
 
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];
 
Reply With Quote
 
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
> 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

 
Reply With Quote
 
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

 
Reply With Quote
 
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
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

 
Reply With Quote
 
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?



 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Question concerning array.array and C++ Fabio Python 0 11-05-2008 03:53 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
"array of Derived" is not a kind-of "array of Base" question Joseph Turian C++ 11 01-19-2005 11:53 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57