Slice returned hash or access to its elements without assigning to a variable

Discussion in 'Perl Misc' started by Andrew, Jun 11, 2005.

  1. Andrew

    Andrew Guest

    Hi,

    Please help me in solving next problem(I search in FAQ bun have not
    find an answer):
    There is a sub foo which return hash:

    sub foo{my %in=( key1=>value1
    key2=>value2
    key3=>value3);
    }
    I need to assess elements of returned hash without assigning to a
    variable. Something like this:
    print @&foo(){'key1','key2'};
    print $&foo(){'key1'};

    But this does not work: "Bareword found where operator expected"

    Thanks,
    Andrew
    Andrew, Jun 11, 2005
    #1
    1. Advertising

  2. Re: Slice returned hash or access to its elements without assigningto a variable

    Andrew wrote:
    > There is a sub foo which return hash:
    >
    > sub foo{my %in=( key1=>value1
    > key2=>value2
    > key3=>value3);
    > }


    Would that be the sub? Don't think so. It doesn't even compile, and even
    if it had compiled, it would have returned a list rather than a hash.

    Subroutines don't return hashes, but they may return references to hashes.

    perldoc perlsub

    > I need to assess elements of returned hash without assigning to a
    > variable. Something like this:
    > print @&foo(){'key1','key2'};
    > print $&foo(){'key1'};


    sub foo {
    my %in = ( key1=>'value1', key2=>'value2', key3=>'value3' );
    \%in
    }

    print @{ foo() }{'key1','key2'};

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 11, 2005
    #2
    1. Advertising

  3. At 2005-06-11 12:35PM, Andrew <> wrote:
    > Hi,
    >
    > Please help me in solving next problem(I search in FAQ bun have not
    > find an answer):
    > There is a sub foo which return hash:
    >
    > sub foo{my %in=( key1=>value1
    > key2=>value2
    > key3=>value3);
    > }


    Strictly speaking, this returns a 6-element list, not a hash.

    > I need to assess elements of returned hash without assigning to a
    > variable. Something like this:
    > print @&foo(){'key1','key2'};
    > print $&foo(){'key1'};
    >
    > But this does not work: "Bareword found where operator expected"


    Coerce the returned list from foo into a hash ref, and dereference
    accordingly:
    sub foo { (a=>1,b=>2,c=>3) }
    $b = +{foo()}->{b};
    @arr = @{%{+{foo()}}}{'a','b'}

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Jun 11, 2005
    #3
  4. At 2005-06-11 12:35PM, Andrew <> wrote:
    > Hi,
    >
    > Please help me in solving next problem(I search in FAQ bun have not
    > find an answer):
    > There is a sub foo which return hash:
    >
    > sub foo{my %in=( key1=>value1
    > key2=>value2
    > key3=>value3);
    > }


    Strictly speaking, this would return a 6-element list, not a hash, if it
    was proper Perl.
    sub foo { (a=>1,b=>2,c=>3) }

    > I need to assess elements of returned hash without assigning to a
    > variable. Something like this:
    > print @&foo(){'key1','key2'};
    > print $&foo(){'key1'};
    >
    > But this does not work: "Bareword found where operator expected"


    Coerce the returned list from foo into a hash ref, and dereference
    accordingly:
    $b = +{foo()}->{b};
    @arr = @{%{+{foo()}}}{'a','b'}

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Jun 11, 2005
    #4
  5. Andrew

    Andrew Guest

    Andrew wrote:
    > Hi,
    >
    > Please help me in solving next problem(I search in FAQ bun have not
    > find an answer):

    ....
    I see.
    Thanks
    Andrew, Jun 11, 2005
    #5
  6. Re: Slice returned hash or access to its elements without assigningto a variable

    Glenn Jackman wrote:

    > Coerce the returned list from foo into a hash ref, and dereference
    > accordingly:


    Good advice

    > @arr = @{%{+{foo()}}}{'a','b'}


    No,

    @arr = @{{foo()}}{'a','b'}

    Due to a bug in the Perl compiler the spurious %{} will be ignored but
    it's still wrong.
    Brian McCauley, Jun 11, 2005
    #6
  7. Andrew

    Guest

    Gunnar Hjalmarsson <> wrote:
    > Subroutines don't return hashes, but they may return references to hashes.


    Well, since a hash being returned gets flattened into a list, there is
    no reason why this list could not be picked up in an assignment to a
    hash, although it would probably not be good practice.

    sub abc {
    ...;
    %bar;
    }

    my %foo = abc();

    Axel
    , Jun 11, 2005
    #7
  8. Re: Slice returned hash or access to its elements without assigningto a variable

    wrote:
    > Gunnar Hjalmarsson wrote:
    >> Andrew wrote:
    >>> There is a sub foo which return hash:

    >>
    >> Subroutines don't return hashes, but they may return references to hashes.

    >
    > Well, since a hash being returned


    Again, a hash is never returned. The last expression that is evaluated
    may be a hash, which is probably what you mean, and in list context a
    hash returns a list of the keys and values. Sorry about this nitpicking,
    Axel, but I believe it is motivated.

    > gets flattened into a list, there is no reason why this list could not
    > be picked up in an assignment to a hash,


    One reason would be the OP's requirement to access the values without
    assigning to a variable. ;-)

    > although it would probably not be good practice.


    Right.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 12, 2005
    #8
  9. At 2005-06-11 03:00PM, Brian McCauley <> wrote:
    > Glenn Jackman wrote:
    > > @arr = @{%{+{foo()}}}{'a','b'}

    > No,
    > @arr = @{{foo()}}{'a','b'}
    > Due to a bug in the Perl compiler the spurious %{} will be ignored but
    > it's still wrong.


    Thanks for the simplication. I was flailing about with braces and types
    trying to construct an example.

    So, what goes into the first set of braces is a hashref, yes?
    %a = (a=>1,b=>2,c=>3);
    $aref = \%a;

    # these are valid:
    $a{b};
    ${a}{b};
    ${$aref}{b};
    $$aref{b};
    ${\%a}{b};

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Jun 13, 2005
    #9
  10. Re: Slice returned hash or access to its elements without assigningto a variable

    Glenn Jackman wrote:
    > At 2005-06-11 03:00PM, Brian McCauley <> wrote:
    >
    >> Glenn Jackman wrote:
    >>
    >>> @arr = @{%{+{foo()}}}{'a','b'}

    >>
    >> No,
    >> @arr = @{{foo()}}{'a','b'}
    >> Due to a bug in the Perl compiler the spurious %{} will be ignored but
    >> it's still wrong.

    >
    >
    > Thanks for the simplication. I was flailing about with braces and types
    > trying to construct an example.
    >
    > So, what goes into the first set of braces is a hashref, yes?


    Yes. (Unless it's a word).

    > %a = (a=>1,b=>2,c=>3);
    > $aref = \%a;
    >
    > # these are valid:
    > $a{b};
    > ${a}{b};
    > ${$aref}{b};
    > $$aref{b};
    > ${\%a}{b};


    Yes.

    Note that in ${a}{b}; the 'a' is treated neither as a bareword string
    nor as a symbolic reference[1]. ${a}{b} is simply an affected way to
    write $a{b}.

    [1] Although historically, in Perl4, you could have choosen to think of
    it that way if you wanted.
    Brian McCauley, Jun 14, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    520
    red floyd
    Nov 10, 2011
  2. thunk
    Replies:
    1
    Views:
    308
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    476
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    617
    thunk
    Apr 3, 2010
  5. David

    Hash slice as hash

    David, Oct 25, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    93
    Paul Lalli
    Oct 25, 2004
Loading...

Share This Page