Home > Archive > PERL Beginners > May 2007 > Array of Array refs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Array of Array refs
|
|
|
| Hi All-
I am trudging through some DBI, XML, etc.. I had a problem and was
baffled by how to get at array elements out of a series of pushed
array refs. But, by simplifying the problem, I found that the syntax I
used was in error. here is the small sample, already debugged. Hope
this helps someone...
#!/usr/bin/perl
my @tRespsA;
my @fieldList = ( "one", "two", "three", "four" );
my @r1 = ( 1, 2, 3, 4 );
my @r2 = ( 13, 14, 15, 16 );
my @r3 = ( 23, 24, 25, 26 );
push @tRespsA, \@r1;
push @tRespsA, \@r2;
push @tRespsA, \@r3;
foreach my $tRowRef ( @tRespsA ) {
my $tCnt=0;
foreach my $tFld (@fieldList) {
#if ( $tRowRef->[ $tCnt] eq "") { next; }
print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
}
}
| |
| Rob Dixon 2007-05-28, 6:58 pm |
| Brian wrote:
> Hi All-
> I am trudging through some DBI, XML, etc.. I had a problem and was
> baffled by how to get at array elements out of a series of pushed
> array refs. But, by simplifying the problem, I found that the syntax I
> used was in error. here is the small sample, already debugged. Hope
> this helps someone...
>
> #!/usr/bin/perl
>
> my @tRespsA;
>
> my @fieldList = ( "one", "two", "three", "four" );
> my @r1 = ( 1, 2, 3, 4 );
> my @r2 = ( 13, 14, 15, 16 );
> my @r3 = ( 23, 24, 25, 26 );
>
> push @tRespsA, \@r1;
> push @tRespsA, \@r2;
> push @tRespsA, \@r3;
>
> foreach my $tRowRef ( @tRespsA ) {
> my $tCnt=0;
> foreach my $tFld (@fieldList) {
> #if ( $tRowRef->[ $tCnt] eq "") { next; }
> print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
> }
> }
First of all, /always/
use strict;
use warnings;
I think you're still a little and thinking in another language - something
like C? Your program works, sure, but iterating over a list of header names in the
inner loop confuses things and won't provide a general solution. Also your cryptic
variable names don't help.
Take a look at this program and see what you think. The variable $i wouldn't be
necessary at all if we weren't displaying header names as well as the array data.
HTH,
Rob
use strict;
use warnings;
my @r1 = ( 1, 2, 3, 4 );
my @r2 = ( 13, 14, 15, 16 );
my @r3 = ( 23, 24, 25, 26 );
my @array2d;
push @array2d, \@r1;
push @array2d, \@r2;
push @array2d, \@r3;
my @heads = qw/ one two three four /;
foreach my $row (@array2d) {
my $i = 0;
foreach my $col (@$row) {
printf "%s = %s\n", $heads[$i++], $col;
}
print "\n";
}
**OUTPUT**
one = 1
two = 2
three = 3
four = 4
one = 13
two = 14
three = 15
four = 16
one = 23
two = 24
three = 25
four = 26
| |
| Paul Lalli 2007-05-28, 6:58 pm |
| On May 28, 12:00 am, googleA...@screenlight.com (Brian) wrote:
> I am trudging through some DBI, XML, etc.. I had a problem and was
> baffled by how to get at array elements out of a series of pushed
> array refs.
What's to be baffled by? Have you read:
perldoc perlreftut
perldoc perllol
perldoc perldsc
?
Paul Lalli
| |
|
| On May 27, 9:00 pm, googleA...@screenlight.com (Brian) wrote:
> Hi All-
> I am trudging through some DBI, XML, etc.. I had a problem and was
> baffled by how to get at array elements out of a series of pushed
> array refs. But, by simplifying the problem, I found that the syntax I
> used was in error. here is the small sample, already debugged. Hope
> this helps someone...
>
> #!/usr/bin/perl
>
> my @tRespsA;
>
> my @fieldList = ( "one", "two", "three", "four" );
> my @r1 = ( 1, 2, 3, 4 );
> my @r2 = ( 13, 14, 15, 16 );
> my @r3 = ( 23, 24, 25, 26 );
>
> push @tRespsA, \@r1;
> push @tRespsA, \@r2;
> push @tRespsA, \@r3;
>
> foreach my $tRowRef ( @tRespsA ) {
> my $tCnt=0;
> foreach my $tFld (@fieldList) {
> #if ( $tRowRef->[ $tCnt] eq "") { next; }
> print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
> }
> }
ok... a few things..
use strict and warnings is fine.. this was a small sample, perhaps
allthe more important to include, actually, so you are right there...
my variable names end with A for array and H for hash, start with a
lower case t if they are used locally as temp variables, as in being
written over each time through a loop. But this isn't a real program,
so there isn't that much detail. In fact, I'd say that starting
variable name s with array again and again is very hard to read.
Resp is short for responses. These aren't actually static arrays, they
come from iterating over the arrays returned by a DBI execute(). this
is simplified for purposes of explanation. The reason for the
construct is that those are names of fields used in formulating the
Query string.
var names rows and cols is fine...
yes, I come from C
yes, I have read all of those perl doc pages. You perl folks like to
refer to whose pages a lot Paul, but in many cases they are not very
well written as manuals [this is provably true] However, to give
credit where credit is due, they are often full of interesting
tidbits, and they do exist, so a lot of people have obviously tried to
do a lot of explaining. ..and yes, I can make mistakes in the syntax,
even though I have read through those. Practice makes perfect.
I do appreciate the responses, as I don't really have anyone else to
ask in my environment.
thank you for that
| |
|
| On May 27, 9:00 pm, googleA...@screenlight.com (Brian) wrote:
> Hi All-
> I am trudging through some DBI, XML, etc.. I had a problem and was
> baffled by how to get at array elements out of a series of pushed
> array refs. But, by simplifying the problem, I found that the syntax I
> used was in error. here is the small sample, already debugged. Hope
> this helps someone...
>
> #!/usr/bin/perl
>
> my @tRespsA;
>
> my @fieldList = ( "one", "two", "three", "four" );
> my @r1 = ( 1, 2, 3, 4 );
> my @r2 = ( 13, 14, 15, 16 );
> my @r3 = ( 23, 24, 25, 26 );
>
> push @tRespsA, \@r1;
> push @tRespsA, \@r2;
> push @tRespsA, \@r3;
>
> foreach my $tRowRef ( @tRespsA ) {
> my $tCnt=0;
> foreach my $tFld (@fieldList) {
> #if ( $tRowRef->[ $tCnt] eq "") { next; }
> print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
> }
> }
oh yes, more important than all that minutiae... the push did not
work for me in the working code. The array was being rewritten. I had
to use an array copy
push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array,
push array ref
| |
| Paul Lalli 2007-05-28, 9:59 pm |
| On May 28, 3:22 pm, googleA...@screenlight.com (Brian) wrote:
> my variable names end with A for array and H for hash,
Pointless. Variable names in Perl identify what kind of variable they
are. @ for arrays, % for hashes. [ ] for accessing an element of a
hash, { } for accessing element of a hash.
> yes, I have read all of those perl doc pages. You perl folks like to
> refer to whose pages a lot Paul, but in many cases they are not very
> well written as manuals [this is provably true]
I heartily disagree, but if you think there's something that's not
clear, either use the built-in `perlbug` utility, mention it to the
perl5-porters list, or start a discussion about it on
comp.lang.perl.misc. That's the point of the language being open-
source, after all. Far more people are going to see those documents
than will ever see your post to beginners@perl.org, so if you're aim
is really to help out new Perl programmers, improving the
documentation is the best approach.
Paul Lalli
| |
| Paul Lalli 2007-05-28, 9:59 pm |
| On May 28, 3:26 pm, googleA...@screenlight.com (Brian) wrote:
> On May 27, 9:00 pm, googleA...@screenlight.com (Brian) wrote:
>
>
>
>
>
>
>
>
>
>
>
> oh yes, more important than all that minutiae... the push did not
> work for me in the working code.
The push worked absolutely fine. It just didn't do what you wanted it
to. Learning how to parse your problem should be your first step
toward becoming a better programmer.
> The array was being rewritten.
Then you didn't delcare your variables in the correct scope. As a
general rule of thumb, declare your variables in the smallest scope
possible.
> I had to use an array copy
>
> push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array,
> push array ref
Do you understand *why* that was necessary? Do you understand the
difference between these two pieces of code?
#Sample 1
my @array;
my @BigArray;
for (1..5) {
@array = get_contents();
push @BigArray(\@array);
}
#Sample 2;
my @BigArray;
for (1..5) {
my @array = get_contents();
push @BigArray(\@array);
}
In the first, you're reusing the same array over and over again. Each
time, you're overwriting the contents of the array with new values,
but you keep pushing references to THE SAME ARRAY onto @BigArray.
In the second, you constantly create and destroy a brand new array
each time through the for loop. The array gets created, it is filled
with contents, a reference to that array is created, that reference is
pushed onto @BigArray, and then that array goes out of scope. The
contents are still accessable via the reference you pushed onto
@BigArray, but via no other means. Next iteration, a brand new array
is created.
This might make it more clear:
my @BigArray;
my @array1;
for (1..3) {
my @array2;
my $ref1 = \@array1;
my $ref2 = \@array2;
push @BigArray, $ref1, $ref2;
print "$ref1 - $ref2\n";
}
__END__
Output:
ARRAY(0x37c38) - ARRAY(0x37c80)
ARRAY(0x37c38) - ARRAY(0x2537c)
ARRAY(0x37c38) - ARRAY(0x25094)
Do you see now? By declaring only one instance of @array1, you're
getting the same array every time you take a reference to it, so
within the for loop, you keep changing the contents of the same
array. @array2, on the other hand, is a completely new and unrelated
array each time through the for loop.
Paul Lalli
| |
|
| On May 28, 6:14 pm, mri...@gmail.com (Paul Lalli) wrote:
>
>
> The push worked absolutely fine. It just didn't do what you wanted it
> to. Learning how to parse your problem should be your first step
> toward becoming a better programmer.
>
hmmm, misunderstanding there. The push worked fine in the sample I
posted, but not in the more complex working program I had simplified
as an example.
>
> Then you didn't delcare your variables in the correct scope.
understandable misperception on your part, as above
> ...As a
> general rule of thumb, declare your variables in the smallest scope
> possible.
>
yes, sounds good. Please realize that after many long commercial
projects in C/C++, we
did this to an enormous degree
>
>
> Do you understand *why* that was necessary? Do you understand the
> difference between these two pieces of code?
>
actually, I do indeed. In C++, the concept of a deep copy, vs
shallow copy vs ref
comes up all the time. I am just learning the syntax here, not
programming itself
> #Sample 1
> ...
hey, thats alot of sample! you're good at this, no kidding
driven too
>
> Do you see now? ...
yep, never did miss that. thanks though...
> ...By declaring only one instance of @array1...
this is helpful to others to talk this out
I'd say this can never be explained too much. Its slippery and a
constant
source of error.
>
> Paul Lalli
aha, a force to be reckoned with. Your point above about the docs is
quite true.
I dont have time to rewrite docs right now.. They do need work though
This is sometimes appearing to be contentious, but I mean it to
produce clarity.
I appreciate your responses
| |
|
| On May 28, 6:04 pm, mri...@gmail.com (Paul Lalli) wrote:
> On May 28, 3:22 pm, googleA...@screenlight.com (Brian) wrote:
>
>
> Pointless. Variable names in Perl identify what kind of variable they
> are. @ for arrays, % for hashes. [ ] for accessing an element of a
> hash, { } for accessing element of a hash.
>
no, I disagree. Changing @ to $ is confusing... besides that, my
naming
is consistent, and helps me understand what I'm writing. I think you
missed it on that one... its a disservice to beginners to say
'pointless' like that..
( I may not respond in such detail in the future.. Its just that I'm
new here,
- I'm surprised by all of this intensity over something pretty
mundane. )
| |
| Paul Lalli 2007-05-29, 6:59 pm |
| On May 29, 5:30 am, googleA...@screenlight.com (Brian) wrote:
> On May 28, 6:04 pm, mri...@gmail.com (Paul Lalli) wrote:
>
>
>
>
> no, I disagree. Changing @ to $ is confusing
@ is for entire arrays. $ is for single elements. How is that
confusing?
>... besides that, my
> naming
> is consistent, and helps me understand what I'm writing. I think you
> missed it on that one
Your method relies on the programmer being consistent. Perl's built-
in method is inforced via the compiler. Your method relies on all
programmers who will ever read or modify your code following the same
convention.
> ... its a disservice to beginners to say 'pointless' like that..
You're right, it's actually worse than pointless, since it's not
enforceable nor guaranteed, and therefore creates a false sense of
security.
Paul Lalli
| |
| Paul Lalli 2007-05-29, 6:59 pm |
| On May 29, 4:58 am, googleA...@screenlight.com (Brian) wrote:
> On May 28, 6:14 pm, mri...@gmail.com (Paul Lalli) wrote:
>
>
>
>
>
> hmmm, misunderstanding there. The push worked fine in the sample I
> posted, but not in the more complex working program I had simplified
> as an example.
nope, sorry, you're wrong. push() works perfectly well. It adds
elements to an array. If your program produced incorrect results, it
is because you did something wrong, not because "push didn't work." Of
course, as you haven't shown any code that produces these undesired
results, we can only guess as to what your actual problem was.
> understandable misperception on your part, as above
Nope. If your array is being used in a loop, the contents of that
array are changing when you don't want them to be changing, and
instead want to be creating new arrays, you declared your array in the
wrong scope.
>
>
>
> actually, I do indeed. In C++, the concept of a deep copy, vs
> shallow copy vs ref
> comes up all the time. I am just learning the syntax here, not
> programming itself
perhaps you should be. . .
> aha, a force to be reckoned with. Your point above about the docs is
> quite true.
> I dont have time to rewrite docs right now.. They do need work though
You don't even have time to point out what you find to be "wrong" with
them? But instead you do have time to create examples that you claim
to be in the service of newbies, all the while saying that the docs
are bad? I'd suggest you could do with attending a few more time-
management seminars.
> This is sometimes appearing to be contentious
No sometimes about it. Every post you've made thus far is
contentious, and so I have answered in kind.
Paul Lalli
| |
|
| On May 29, 6:06 am, mri...@gmail.com (Paul Lalli) wrote:
> On May 29, 4:58 am, googleA...@screenlight.com (Brian) wrote:
>
>
>
>
>
> nope, sorry, you're wrong. push() works perfectly well. It adds
> elements to an array. If your program produced incorrect results, it
> is because you did something wrong, not because "push didn't work."
ugh. pedantic semantics...
> Of
> course, as you haven't shown any code that produces these undesired
> results, we can only guess as to what your actual problem was.
yes, the real code is beyond what I would think of as "beginner"
I truly meant to just post a small, working piece pf code that worked
with
some basic data structures... The DBI client is a bit more
complicated, yes?
>
>
> Nope. If your array is being used in a loop, the contents of that
> array are changing when you don't want them to be changing, and
> instead want to be creating new arrays, you declared your array in the
> wrong scope.
>
The sample I put out and the code I was
working on are not identical. I declared my array in a different scope
in
the sample. Neither is "wrong", they are different.
>
>
>
>
> perhaps you should be. . .
ugh. insults. I am not trying to insulting you Paul, why resort to
that?
You have no idea how many programs I've written, well. I'd say its bad
form
to assume the worst or lowest in people.
>
>
> You don't even have time to point out what you find to be "wrong" with
> them? But instead you do have time to create examples that you claim
> to be in the service of newbies, all the while saying that the docs
> are bad? I'd suggest you could do with attending a few more time-
> management seminars.
>
please examine logic - rewriting core Perl docs vs a 10 line sample
program
>
> No sometimes about it. Every post you've made thus far is
> contentious, and so I have answered in kind.
>
no, I'd suggest you read it that way.. Its notoriously difficult to
convey context
and nuance in a few lines of ascii text. Please refer to your RTFM and
then
essentially yelling at me for a modest sample program.
I am not backing down from your brow-beating. I posted a small
working
sample program then found out more about the context I was dealing
with
and attempted to discuss it in an unassuming manner. For this I get
these
responses. phooey!
I'll be posting again from time to time, and probably going to OSCon.
Happy
to talk with you anytime. May not respond every time though. I think I
am
sensing a pattern here...
best regards
-Brian
| |
| Andrew Curry 2007-05-30, 3:58 am |
| Enough Is enough, can we leave this thread be now. This just puts people off
posting questions looking for help in fear of joining some flame war.
-----Original Message-----
From: Brian [mailto:googleAcct@screenlight.com]
Sent: 29 May 2007 19:30
To: beginners@perl.org; perl-beginners@moderators.isc.org
Subject: Re: Array of Array refs
On May 29, 6:06 am, mri...@gmail.com (Paul Lalli) wrote:
> On May 29, 4:58 am, googleA...@screenlight.com (Brian) wrote:
>
>
>
>
>
> nope, sorry, you're wrong. push() works perfectly well. It adds
> elements to an array. If your program produced incorrect results, it
> is because you did something wrong, not because "push didn't work."
ugh. pedantic semantics...
> Of
> course, as you haven't shown any code that produces these undesired
> results, we can only guess as to what your actual problem was.
yes, the real code is beyond what I would think of as "beginner"
I truly meant to just post a small, working piece pf code that worked with
some basic data structures... The DBI client is a bit more complicated, yes?
>
>
> Nope. If your array is being used in a loop, the contents of that
> array are changing when you don't want them to be changing, and
> instead want to be creating new arrays, you declared your array in the
> wrong scope.
>
The sample I put out and the code I was
working on are not identical. I declared my array in a different scope in
the sample. Neither is "wrong", they are different.
>
>
>
>
> perhaps you should be. . .
ugh. insults. I am not trying to insulting you Paul, why resort to that?
You have no idea how many programs I've written, well. I'd say its bad form
to assume the worst or lowest in people.
>
>
> You don't even have time to point out what you find to be "wrong" with
> them? But instead you do have time to create examples that you claim
> to be in the service of newbies, all the while saying that the docs
> are bad? I'd suggest you could do with attending a few more time-
> management seminars.
>
please examine logic - rewriting core Perl docs vs a 10 line sample program
>
> No sometimes about it. Every post you've made thus far is
> contentious, and so I have answered in kind.
>
no, I'd suggest you read it that way.. Its notoriously difficult to convey
context and nuance in a few lines of ascii text. Please refer to your RTFM
and then essentially yelling at me for a modest sample program.
I am not backing down from your brow-beating. I posted a small working
sample program then found out more about the context I was dealing with and
attempted to discuss it in an unassuming manner. For this I get these
responses. phooey!
I'll be posting again from time to time, and probably going to OSCon.
Happy
to talk with you anytime. May not respond every time though. I think I am
sensing a pattern here...
best regards
-Brian
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org http://learn.perl.org/
This e-mail is from the PA Group. For more information, see
www.thepagroup.com.
This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.
Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.
|
|
|
|
|