I was able to print with the below line. Be sure that you are passing a value 
0-2 as an argument to your script since you have only three elements in your 
array.

print "$object->[$stp]{'stp'}\n";

Regards,
Sachin

-----Original Message-----
From: HACKER Nora [mailto:nora.hac...@stgkk.at] 
Sent: Monday, February 21, 2011 2:17 PM
To: beginners@perl.org
Subject: Access single element of AOH

Hello,

I want to get the value of a key of a Hash that is part of an Array of
Hashes, whereas the correct hash to work with is being determined by the
value of another key which is passed as an argument when calling the
script, but haven't yet figured out how. This is my AOH:

#!/usr/bin/perl 
use strict;
use warnings;

my $stp = $ARGV[0];

my $object = [
                {   'stp'       => 'AV'
                  , 'lieferung' => 'D:\mvbwiega\stp_be\LIEFERUNG'
                  , 'hvb'       => 'H:\stp-be\LIEFERUNG'
                  , 'tux'       => 'Releaseschein-2004\Server\Tuxedo'
                  , 'ubbconfig' => 'beispiel_ubbconfig.txt'
                },
                {   'stp'       => 'BE'
                  , 'lieferung' => 'D:\mvbwiega\stp_be\LIEFERUNG'
                  , 'hvb'       => 'H:\stp-be\LIEFERUNG'
                  , 'tux'       => 'Releaseschein-2004\Server\Tuxedo'
                  , 'ubbconfig' => 'beispiel_ubbconfig.txt'
                },
                {   'stp'       => 'PKV'
                  , 'lieferung' => 'D:\mvbwiega\stp_pkv\Releases'
                  , 'hvb'       => 'H:\stp-pkv\Releases'
                  , 'tux'       => 'RS_2004\Tuxedo'
                  , 'ubbconfig' => 'beispiel_ubbconfig.txt'
                }
             ];

And these were (some of) my failing attempts:

print "$object{'lieferung'}{$stp}\n";
==>     Global symbol "%object" requires explicit package name at
./test.pl line 71.
==>     Execution of ./test.pl aborted due to compilation errors.

print "$object->[$stp]{'lieferung'}\n";
==>     Argument "AV" isn't numeric in array element at ./test.pl line
72.
==>     D:\mvbwiega\stp_be\LIEFERUNG    # always result of first hash,
no matter which parameter given

print "$object->{$stp}{'lieferung'}\n";
==>     Pseudo-hashes are deprecated at ./test.pl line 72.
==>     No such pseudo-hash field "AV" at ./test.pl line 72.

Somehow I only seem to find examples/explanations with 'foreach'es,
looping over the whole AOH ... Is it even possible what I want to
realize? Or do I have to change my data structure to a Hash of Hashes,
would that be better/easier?

Thanks in advance, 
Nora 




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to