Ask a Question related to PERL Beginners, Design and Development.
-
Paul Harwood #1
Array de-referencing problem
I wrote the following code:
$thread = "TEFD0";
$logfile = "V_DEBUG12121.txt";
$delay = "10232";
@servers = qw (SERVER1 SERVER2 SERVER3 SERVER4);
$server {$thread}->{$logfile}->{$delay} = @servers;
print @$server {$thread}->{$logfile}->{$delay};
My expectation is that it would simply print out the list value of
@servers. Instead I get an error: Can't use an undefined value as a HASH
reference.
If I use: print $server {$thread}->{$logfile}->{$delay};
Then I get the scalar context of the # of values in @servers - 4. So I
know I am missing something obvious. Since the value I am looking for is
the contents of an array, why do I get that error?
--Paul
Paul Harwood Guest
-
Referencing array results returned to Flex via a RemoteObject Call
Hi, Here's the RO result using the NetConnection Debugger. I have a function that can always read the first array element, but the others... -
referencing html form array
I'm trying to collect the values from an html form array. For example: <input type='text' name='order' value='4'> <input type='text' name='order'... -
Problem referencing web service
Hi I am trying to get web reference to my first (!) web service which is on a remote host. I am getting the following error; Server Error in... -
referencing problem, or so i think
// Hi I can't seem to pass the right instance to the function move Make_bg() //initiate... -
array behaviour - referencing past last element
>>>>> "h" == haldane <jbshaldane@hotmail.com> writes: h> Both a and a use out of range indices, so why is there a h> difference? The... -
Jenda Krynicky #2
Re: Array de-referencing problem
From: "Paul Harwood" <harwood@nyclimits.org>
The first problem is here. You are evaluating the array in scalar> I wrote the following code:
>
> $thread = "TEFD0";
> $logfile = "V_DEBUG12121.txt";
> $delay = "10232";
> @servers = qw (SERVER1 SERVER2 SERVER3 SERVER4);
> $server {$thread}->{$logfile}->{$delay} = @servers;
context and storing the resut (the length of the array) into the
hash.
The line above is equivalent to
$length = @servers;
$server {$thread}->{$logfile}->{$delay} = $length;
The values of a hash can only be scalars!
This means that you have to store a reference to the array in the
hash:
$server{$thread}->{$logfile}->{$delay} = \@servers;
The problem in this case is that perl thinks that you want to use the> print @$server {$thread}->{$logfile}->{$delay};
>
> My expectation is that it would simply print out the list value of
> @servers. Instead I get an error: Can't use an undefined value as a
> HASH reference.
$server as a reference to a hash (or a name of a hash variable in
your case), then take a hash slice, and then use that hash slice as a
reference to hash.
That is it treats the line like this:
print @{$server}{$thread}->{$logfile}->{$delay};
while what you want is
print @{$server{$thread}->{$logfile}->{$delay}};
P.S.: You've accidentaly used "symbolic references". They are a bad
idea usualy even if you do want to use them, but in this case they
just confused the matter. You DO want to
use strict;
use warnings;
no warnings 'uninitialized';
on top of all your scripts.
Jenda
===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Jenda Krynicky Guest
-
Paul Harwood #3
RE: Array de-referencing problem
Thanks!
I got it to work using:
$server {$thread}->{$logfile}->{$delay} = [@servers];
But I guess it's better to use an array reference?
-----Original Message-----
From: Jenda Krynicky [mailto:Jenda@Krynicky.cz]
Posted At: Friday, November 21, 2003 3:37 AM
Posted To: Perl
Conversation: Array de-referencing problem
Subject: Re: Array de-referencing problem
From: "Paul Harwood" <harwood@nyclimits.org>The first problem is here. You are evaluating the array in scalar> I wrote the following code:
>
> $thread = "TEFD0";
> $logfile = "V_DEBUG12121.txt";
> $delay = "10232";
> @servers = qw (SERVER1 SERVER2 SERVER3 SERVER4);
> $server {$thread}->{$logfile}->{$delay} = @servers;
context and storing the resut (the length of the array) into the
hash.
The line above is equivalent to
$length = @servers;
$server {$thread}->{$logfile}->{$delay} = $length;
The values of a hash can only be scalars!
This means that you have to store a reference to the array in the
hash:
$server{$thread}->{$logfile}->{$delay} = \@servers;
The problem in this case is that perl thinks that you want to use the> print @$server {$thread}->{$logfile}->{$delay};
>
> My expectation is that it would simply print out the list value of
> @servers. Instead I get an error: Can't use an undefined value as a
> HASH reference.
$server as a reference to a hash (or a name of a hash variable in
your case), then take a hash slice, and then use that hash slice as a
reference to hash.
That is it treats the line like this:
print @{$server}{$thread}->{$logfile}->{$delay};
while what you want is
print @{$server{$thread}->{$logfile}->{$delay}};
P.S.: You've accidentaly used "symbolic references". They are a bad
idea usualy even if you do want to use them, but in this case they
just confused the matter. You DO want to
use strict;
use warnings;
no warnings 'uninitialized';
on top of all your scripts.
Jenda
===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Paul Harwood Guest
-
Jenda Krynicky #4
RE: Array de-referencing problem
From: "Paul Harwood" <harwood@nyclimits.org>
You are using an array reference.> Thanks!
>
> I got it to work using:
>
> $server {$thread}->{$logfile}->{$delay} = [@servers];
>
> But I guess it's better to use an array reference?
The difference is that
$server {$thread}->{$logfile}->{$delay} = [@servers];
creates a copy of the array and stores the reference to the copy
while
$server {$thread}->{$logfile}->{$delay} = \@servers;
stored copy to the original array.
Which of these two do you need I'm not sure.
Try this:
@a = (1,2,3);
$ref1 = \@a;
$ref2 = [@a];
print "(@$ref1) == (@$ref2)\n";
$a[0] = 99;
print "(@$ref1) != (@$ref2)\n";
HTH, Jenda
===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Jenda Krynicky Guest



Reply With Quote

