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!
Re: Array Question
am 10.09.2007 16:48:02 von Benoit Lefebvre
On Sep 10, 10:38 am, "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!
On Sep 10, 10:38 am, "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???
print "(0,2) = $x[0][2]\n";
You need to read up on creating multidimensional structures in Perl:
perldoc perlreftut
perldoc perllol
Paul Lalli
Re: Array Question
am 10.09.2007 16:53:41 von Paul Lalli
On Sep 10, 10:48 am, Benoit Lefebvre
wrote:
> On Sep 10, 10:38 am, "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:
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
Re: Array Question
am 10.09.2007 16:55:24 von amerar
On Sep 10, 9:51 am, Paul Lalli wrote:
> On Sep 10, 10:38 am, "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:
On Sep 10, 10:53 am, Paul Lalli wrote:
> On Sep 10, 10:48 am, Benoit Lefebvre
> wrote:
>
>
>
> > On Sep 10, 10:38 am, "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:
>
> 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
Re: Array Question
am 10.09.2007 17:18:59 von Benoit Lefebvre
On Sep 10, 10:55 am, "ame...@iwc.net" wrote:
> On Sep 10, 9:51 am, Paul Lalli wrote:
>
>
>
> > On Sep 10, 10:38 am, "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
Re: Array Question
am 10.09.2007 17:21:21 von Paul Lalli
On Sep 10, 10:55 am, "ame...@iwc.net" 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:
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
Re: Array Question
am 10.09.2007 17:24:26 von amerar
On Sep 10, 10:21 am, Paul Lalli wrote:
> On Sep 10, 10:55 am, "ame...@iwc.net" 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?
Re: Array Question
am 10.09.2007 18:03:33 von Uri Guttman
>>>>> "BL" == Benoit Lefebvre writes:
>> > see:
>>
>> 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 ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Re: Array Question
am 10.09.2007 18:10:20 von Paul Lalli
On Sep 10, 11:24 am, "ame...@iwc.net" 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
Re: Array Question
am 10.09.2007 18:13:19 von amerar
On Sep 10, 11:10 am, Paul Lalli wrote:
> On Sep 10, 11:24 am, "ame...@iwc.net" 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.
Re: Array Question
am 10.09.2007 19:39:14 von Jim Gibson
In article <1189440799.026648.28280@19g2000hsx.googlegroups.com>,
<"amerar@iwc.net"> wrote:
> On Sep 10, 11:10 am, Paul Lalli wrote:
> > perldoc perlreftut
> > perldoc perllol
>
> 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.
Read 'perldoc perllol' and search for the section titled 'Access and
Printing'. It has examples on accessing an array of arrays (your
problem), including the following:
for $i ( 0 .. $#AoA ) {
for $j ( 0 .. $#{$AoA[$i]} ) {
print "elt $i $j is $AoA[$i][$j]\n";
}
}
After reading that, post any questions you have about that text.
amerar@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???
>
At first of all - Perl arrays are numbered from 0, not from 1.