You cannot pass a string as an argument since you have an AOH, i.e. an array
containing hash references. Arrays are indexed starting from 0.
If you want to pass a string, then you will need to construct HOH i.e Hash of
Hash and then print the object as
print "$object->{$stp}->{'stp'}\n";
Hope this clarifies.
Regards,
Sachin
-----Original Message-----
From: HACKER Nora [mailto:[email protected]]
Sent: Monday, February 21, 2011 2:39 PM
To: Malesha, Sachin; [email protected]
Subject: AW: Access single element of AOH
Hi,
Thanks for your reply. The argument I want to pass on is not numeric but a
string - is that possible?
Kind regards,
Nora
> -----Ursprüngliche Nachricht-----
> Von: Malesha, Sachin [mailto:[email protected]]
> Gesendet: Montag, 21. Februar 2011 09:54
> An: HACKER Nora
> Cc: [email protected]
> Betreff: RE: Access single element of AOH
>
> 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:[email protected]]
> Sent: Monday, February 21, 2011 2:17 PM
> To: [email protected]
> 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: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/