First of all, don't forget to 'use strict;' at the top of your script.

But here's a couple of scenarios.  The last two are what you are looking
for.  Always remember to put curly braces around the reference when you are
dereferencing, and you won't run into this problem.  Below are some common
scenarios that people put to Perl, and what it sees.  Hopefully it helps you
to see why your original attempts were incorrect.

###########################################

You type:
  "@$avg_resp_time[0]" or "@{$avg_resp_time[0]}"
Perl sees:
  @avg_resp_time is an array, whose first element is an array reference.
Dereference it.

You type:
  @$avg_resp_time->[0]
Perl sees:
  $avg_resp_time is an array reference.  Dereference it and find the first
element, which is also an array reference.  Dereference it.

You type:
  @{$avg_resp_time}[0]
Perl sees:
  $avg_resp_time is an array reference.  Dereference it, and then get a hash
slice that has only the first element.

You type:
  ${$avg_resp_time}[0]
Perl sees:
  $avg_resp_time is an array reference.  Get the first element of the
dereferenced array.

You type:
  $avg_resp_time->[0]
Perl sees:
  $avg_resp_time is an array reference.  Get the first element of the
dereferenced array.

##############################################

-----Original Message-----
From: Zachary Buckholz [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 09, 2002 11:31 PM
To: [EMAIL PROTECTED]
Subject: help dereferencing arrayref so I can put the value into a hash


I understand how to use a foreach on a reference to an array as follows:

my $avg_resp_time = get_avg_resp_time($durations, $url_id);
  foreach my $avg_resp(@$avg_resp_time) {
    print "AVG = $avg_resp\n";
  }

But how do I directly access one array value from the reference to the
array?

print "DEBUG TEST @$avg_resp_time[0]\n";        Fails
print "DEBUG TEST @$avg_resp_time->[0]\n";    Fails
print "DEBUG TEST @{$avg_resp_time[0]}\n";    Fails


I have bookmarked ch04_07.htm of the Perl CD "Data Structure Code Examples"
but I just don't understand it.

I am trying to setup a hash for use in HTML::Template as follows

my $email_vars = { chk_freq             =>      $chk_freq,
                   url_timeout          =>      $url_timeout,
                   url                  =>      $url,
                   url_protocol         =>      $url_protocol,
                   mc_email             =>      $mc_email,
                   full_detail          =>      $full_detail,
                   avg_resp_time        =>      [ qw(@$avg_resp_time[0]
                                                     @$avg_resp_time[1]
                                                     @$avg_resp_time[2]
                                                     @$avg_resp_time[3]
                                                     @$avg_resp_time[4]
                                                     @$avg_resp_time[5]
                                                     @$avg_resp_time[6]) ],
                   sum_errors           =>      $sum_errors,
                   pct_up_time          =>      $pct_up_time,
                                }
Thanks
zack



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to