|
|
Subject: Re:Perl Array Question - msg#01907
List: lang.perl.beginners
> while (my @RACT_LOG_data=$sth_RACT_LOG->fetchrow_array){
>
> $count_RACT_LOG_data++;
> push our @KEY_SNR_RACT_LOG[$i], $RACT_LOG_data[0];
> push our @DRAWING_RE_RACT_LOG[$i], $RACT_LOG_data[1];
> push our @RCODE_ID_RACT_LOG[$i], $RACT_LOG_data[2];
> }
You missed a } for the for loop or missed it for the while loop.
> Thats how I try to get data out of my Arrays...
> for (my $i=0;$i<=$count_SNR_LOG_DATA,$i++){
> for (my $u=0;$u<=$count_RACT_LOG_data;$u++){
> print "$KEY_SNR_RACT_LOG[$i][$u]\n";
> print "$DRAWING_RE_RACT_LOG[$i][$u]\n";
> print "$RCODE_ID_RACT_LOG[$i][$u]\n";
This is a calling to an Array Of Array, but you aren't construct
an AOA during the while loop....
Rgds,
Connie
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
RE: chr help
Hi -
I built a tiny .doc file with MS Word 2000 containing the text:
hello - world
Word did change the dash to a "long" dash on the screen.
Next, I opened the .doc file in binary mode (I my case, using the
C++ built-in editor) and found that line:
48 65 6C 6C 6F 20 96 20 77 6F 72 6C 64 0D 0D 0D
-or-
h e l l o -- w o r l d
So the long dash is hex 96 or chr (150)
Aloha => Beau.
-----Original Message-----
From: Gabby Dizon [mailto:gdizon@xxxxxxxx]
Sent: Tuesday, July 30, 2002 8:23 PM
To: beginners@xxxxxxxx
Subject: chr help
Hi,
I have a subroutine that finds commonly found Unicode characters that
Microsoft Word uses (such as "smart quotes") and converts them to plain
ASCII text. However, there is one character whose chr() value I can't seem
to find - the MS Word long dash, which automatically replaces the normal
(short dash) when you press Enter. Does anyone know or can point me to a
location where I can find the chr() value of this character? All replies are
greatly appreciated.
Gabby Dizon
Web Developer
INQ7 Interactive, Inc.
http://www.inq7.net
http://you.inq7.net
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
Next Message by Date:
click to view message preview
Re:chr help
> I have a subroutine that finds commonly found Unicode characters that
> Microsoft Word uses (such as "smart quotes") and converts them to plain
> ASCII text. However, there is one character whose chr() value I can't seem
> to find - the MS Word long dash, which automatically replaces the normal
> (short dash) when you press Enter. Does anyone know or can point me to a
> location where I can find the chr() value of this character? All replies are
> greatly appreciated.
my @chars = split //, $line; # $line is your string.
foreach my $char(@chars) { print sprintf"%1x", ord($char) ; print " " }
# Hope this help.
Rgds,
Connie
Previous Message by Thread:
click to view message preview
Re: Perl Array Question
First, something more specific than 'this isn't working at all', will
help in diagnosing the problem...but here are some comments which may
help:
Theuerkorn Johannes <Johannes.Theuerkorn@xxxxxxxxxxxxxx> writes:
> Hi there, i have (a probably simple) Question on Arrays:
>
> I want to get some Data out of my SQL Databas. I try using the same Query not
> only once and put the collected data in diffrent Arrays.
>
>
> for (my $i=0;$i<= $count_SNR_LOG_data;$i++){
> print "$i\n";
> my $sth_RACT_LOG = $dbh->prepare ("select KEY_SNR,DRAWING_RE,RCODE_ID
> from RACT_LOG where KEY_SNR='$KEY_SNR_SNR_LOG[$i]';");
First, the $dbh->prepare statement should go before the 'for', like
so:
'
my $sth_RACT_LOG = $dbh->prepare('select KEY_SNR, DRAWING_RE, RCODE_ID
from RACT_LOG where KEY_SNR = ?');
for (my $i = 0; $i <= $count_SNR_LOG_data; $i++) {
print "$i\n";
'
I've also used a placeholder value (the '?') - this takes care of
quoting, and, with a good database module, will speed the queries
significantly. Search `perldoc DBI` for info.
Also, if you are looping through all of the elements in the
'@KEY_SNR_SNR_LOG' array, and assuming they are unique, it would be
better to use foreach:
'
foreach my $elem (@KEY_SNR_SNR_LOG) {
'
This way '$elem' gets the value of each element in the array.
#--back to your code -
> $sth_RACT_LOG->execute ||
> die "Kann Abfrage sth_RACT_LOG nicht ausfuehren:
> $DBI::errstr\n";
call execute with the value to replace the ? with - if you are using a
foreach loop as above, that would be '$elem', if you stick with the
for loop, it would be:
'
$sth_RACT_LOG->execute($KEY_SNR_SNR_LOG[$i]) || die "something
german-sounding";
'
> while (my @RACT_LOG_data=$sth_RACT_LOG->fetchrow_array){
>
> $count_RACT_LOG_data++;
> push our @KEY_SNR_RACT_LOG[$i], $RACT_LOG_data[0];
> push our @DRAWING_RE_RACT_LOG[$i], $RACT_LOG_data[1];
> push our @RCODE_ID_RACT_LOG[$i], $RACT_LOG_data[2];
> }
There are a few problems with this:
First, I would give each value it's own variable - an array just
confusing things here:
'
while (my ($key_snr, $drawing_re, $rcode_id) = $sth->fethrow_array) {
'
Second, I don't see why 'our' is necessary - and if it is, I think the
proper usage would be to call 'our @KEY_SNR_RACT_LOG' (etc) before the
while loop. I've never really had a use for 'our', so perhaps someone
else could shed more light on the situation.
Third, assuming you were to switch to a 'foreach' (and still assuming
all the element in '@KEY_SNR_SNR_LOG' are unique), use hashes:
'
my %key_log;
my %drawing_log;
my %rcode_log; #somewhere above these loops
# ...
push @{$key_log{$elem}}, $key_snr;
push @{$drawing_log{$elem}}, $drawing_re;
push @{$rcode_log{$elem}}, $rcode_id;
'
This says, basically: 'The value of the %key_log hash for key '$elem'
is an array reference. Push '$key_snr' onto the end of that
array.'
Later, you would call the elements of the arrays like so: 'print
$key_log{$elem}->[0]', or the whole array like 'my @foo =
@{$key_log{$elem}}'.
If you feel you must use arrays, what you probably mean to do is this:
'
push @{$KEY_SNR_RACT_LOG[$i]}, $RACT_LOG_data[0];
'
Which says 'The value of element $i of array '@KEY_SNR_SNR_LOG' is an
array reference, push $RACT_LOG_data[0] onto the end of that array.
What you have up there is a slice. Read the Camel book for more
information about them. For now, just use one of the methods I
suggest above.
Fourth, the variable '$count_RACT_LOG_data' is almost certainly
useless for you, and definately useless for what you use it for
below. You are counting the total number of times the query returned
data - over all of the '$sth->executes'.
> Thats how I try to get data out of my Arrays...
>
> for (my $i=0;$i<=$count_SNR_LOG_DATA,$i++){
> for (my $u=0;$u<=$count_RACT_LOG_data;$u++){
> print "$KEY_SNR_RACT_LOG[$i][$u]\n";
> print "$DRAWING_RE_RACT_LOG[$i][$u]\n";
> print "$RCODE_ID_RACT_LOG[$i][$u]\n";
> }
> }
As I said above, part of the problem is that '$count_RACT_LOG_data'
contains the total number of times $sth->fetchrow returned
data...which goes way out of bounds for each individual array.
Re-written with hashes, as recommended in #3 above:
'
foreach my $elem (@KEY_SNR_SNR_LOG) {
for (my $u = 0; $u < @{$key_log{$elem}}; $u++) {
print $key_log{$elem}->[$u] . "\n";
print $drawing_log{$elem}->[$u] . "\n";
print $rcode_log{$elem}->[$u] . "\n";
}
}
'
I've done some not-so-nice things here, and would probably rewrite the
output part entirely, depending on how I needed to access the data,
but this way is pretty much identical to your way, with hashes. The
key for this part is not to use '$count_RACT_log' - use either the
number of elements in the aray '$u < @KEY_SNR_RACT_LOG', or the index
of the last element in the array '$u <= $#KEY_SNR_RACT_LOG' in the
inner for loop. The second method is much more correct, actually.
There are better ways to do this, but this post is getting long enough
as it is. I would recommend a good look at the references and data
structures chapters of 'Programming Perl'. They should help with
understanding how to deal with more complicated data like this.
Wow, that was a long post. Hope it made sense...
All of this code is pretty much completely untested. Beware typos,
logic errors, and gremlins.
-RN
--
Robin Norwood
Red Hat, Inc.
"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching
Next Message by Thread:
click to view message preview
Editor
Hi,
I am new to Perl just over a month now. I have tried other programming
languages and they just seem to hard to understand. I have found Perl to be a
lot easier to understand, and that brings me to my question. What is a good
Perl Editor for writing scripts? I am currently using Crimson Editor.
Thanks
Scott Barnett
Home Care Medical - Technical Support Specialist
1-800-369-6939
1-262-786-9870 ext.214
E-Mail sbarnett@xxxxxxxxxxxxx
|
|