FAQ
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

Search Discussions

  • Malesha, Sachin at Feb 21, 2011 at 8:56 am
    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
    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
  • HACKER Nora at Feb 21, 2011 at 9:09 am
    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
    Gesendet: Montag, 21. Februar 2011 09:54
    An: HACKER Nora
    Cc: beginners@perl.org
    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
    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

  • Malesha, Sachin at Feb 21, 2011 at 9:19 am
    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
    Sent: Monday, February 21, 2011 2:39 PM
    To: Malesha, Sachin; beginners@perl.org
    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
    Gesendet: Montag, 21. Februar 2011 09:54
    An: HACKER Nora
    Cc: beginners@perl.org
    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
    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

  • HACKER Nora at Feb 21, 2011 at 9:33 am
    Perfectly clear, thanks, works already :-)

    Kind regards,
    Nora


    -----Ursprüngliche Nachricht-----
    Von: Malesha, Sachin
    Gesendet: Montag, 21. Februar 2011 10:16
    An: HACKER Nora; beginners@perl.org
    Betreff: RE: Access single element of AOH

    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
    Sent: Monday, February 21, 2011 2:39 PM
    To: Malesha, Sachin; beginners@perl.org
    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
    Gesendet: Montag, 21. Februar 2011 09:54
    An: HACKER Nora
    Cc: beginners@perl.org
    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
    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

  • Parag Kalra at Feb 21, 2011 at 9:00 am
    print $object->[$stp]->{'lieferung'},"\n";

    ~Parag


    On Mon, Feb 21, 2011 at 12:46 AM, HACKER Nora wrote:

    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


  • HACKER Nora at Feb 21, 2011 at 9:09 am
    (forwarded because forgot to „reply all”)





    Von: HACKER Nora
    Gesendet: Montag, 21. Februar 2011 10:08
    An: 'Parag Kalra'
    Betreff: AW: Access single element of AOH



    Hi,



    Thanks for your reply, but it does not work. It produces an error and returns a wrong result.



    oracle:/opt/data/magna/wartung/work/nora> ./test.pl PKV

    Argument "PKV" isn't numeric in array element at ./test.pl line 72.

    D:\mvbwiega\stp_be\LIEFERUNG



    Kind regards,
    Nora





    Von: Parag Kalra
    Gesendet: Montag, 21. Februar 2011 10:00
    An: HACKER Nora
    Cc: beginners@perl.org
    Betreff: Re: Access single element of AOH



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


    ~Parag



    On Mon, Feb 21, 2011 at 12:46 AM, HACKER Nora wrote:

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 21, '11 at 8:46a
activeFeb 21, '11 at 9:33a
posts7
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase