Array de-referencing problem

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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'...
    3. 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...
    4. referencing problem, or so i think
      // Hi I can't seem to pass the right instance to the function move Make_bg() //initiate...
    5. 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...
  3. #2

    Default Re: Array de-referencing problem

    From: "Paul Harwood" <harwood@nyclimits.org>
    > I wrote the following code:
    >
    > $thread = "TEFD0";
    > $logfile = "V_DEBUG12121.txt";
    > $delay = "10232";
    > @servers = qw (SERVER1 SERVER2 SERVER3 SERVER4);
    > $server {$thread}->{$logfile}->{$delay} = @servers;
    The first problem is here. You are evaluating the array in scalar
    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;
    > 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.
    The problem in this case is that perl thinks that you want to use the
    $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

  4. #3

    Default 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>
    > I wrote the following code:
    >
    > $thread = "TEFD0";
    > $logfile = "V_DEBUG12121.txt";
    > $delay = "10232";
    > @servers = qw (SERVER1 SERVER2 SERVER3 SERVER4);
    > $server {$thread}->{$logfile}->{$delay} = @servers;
    The first problem is here. You are evaluating the array in scalar
    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;
    > 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.
    The problem in this case is that perl thinks that you want to use the
    $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

  5. #4

    Default RE: Array de-referencing problem

    From: "Paul Harwood" <harwood@nyclimits.org>
    > Thanks!
    >
    > I got it to work using:
    >
    > $server {$thread}->{$logfile}->{$delay} = [@servers];
    >
    > But I guess it's better to use an array reference?
    You are using 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139