return hash

Discussion in 'Perl' started by vertigo, Jul 15, 2004.

  1. vertigo

    vertigo Guest

    Hello
    at the end of my function i return hash:
    return \%hash;

    and i want to use that function to receive that hash:
    my %newhash=MyFunction(); #line 14

    but i receive error:
    Reference found where even-sized list expected at line 14.

    and when i try:
    my \%newhash=MyFunction(); #line 14
    i receive syntax error.

    Why ?

    Thanx
    Michal
    vertigo, Jul 15, 2004
    #1
    1. Advertising

  2. vertigo wrote:
    > Hello
    > at the end of my function i return hash:
    > return \%hash;


    No, you don't. You are returning a reference.

    > and i want to use that function to receive that hash:
    > my %newhash=MyFunction(); #line 14
    >
    > but i receive error:
    > Reference found where even-sized list expected at line 14.


    Right. You cannot assign a scalar value to a hash variable.

    > and when i try:
    > my \%newhash=MyFunction(); #line 14
    > i receive syntax error.
    >
    > Why ?


    Because this is not Perl syntax.

    Have you tried a plain
    my $hashref = MyFunction();

    jue
    Jürgen Exner, Jul 15, 2004
    #2
    1. Advertising

  3. vertigo

    vertigo Guest


    > Have you tried a plain
    > my $hashref = MyFunction();


    ok, it works, but how can i use this reference to printf all keys/values ?
    I tried:
    while (my($key,$value) = each $hashref)

    but received error. How can i access object when i have it's reference ?

    Thanx
    Michal
    vertigo, Jul 15, 2004
    #3
  4. vertigo wrote:
    >> Have you tried a plain
    >> my $hashref = MyFunction();

    >
    > ok, it works, but how can i use this reference to printf all
    > keys/values ? I tried:
    > while (my($key,$value) = each $hashref)
    >
    > but received error. How can i access object when i have it's
    > reference ?


    You use rule 1 or rule 2 from "perldoc perlreftut".
    If you are dealing with references, then you really should read this first,
    and then perldoc perlref.

    Having said that: in your original program, why are you returning a
    reference in the first place? If you are not interested in a reference to
    the hash, then just return the hash itself instead of a reference to it.

    jue
    Jürgen Exner, Jul 15, 2004
    #4
  5. vertigo

    Jeff Dunn Guest

    vertigo <> wrote in message news:<cd65v0$roh$>...
    > Hello
    > at the end of my function i return hash:
    > return \%hash;
    >
    > and i want to use that function to receive that hash:
    > my %newhash=MyFunction(); #line 14
    >
    > but i receive error:
    > Reference found where even-sized list expected at line 14.
    >
    > and when i try:
    > my \%newhash=MyFunction(); #line 14
    > i receive syntax error.
    >
    > Why ?
    >
    > Thanx
    > Michal



    Do you want to return the hash or a reference to a hash


    return %hash #returns hash
    %hash=func();


    return \$hash retruns a reference.
    $hashRef=func();
    Jeff Dunn, Jul 15, 2004
    #5
  6. vertigo <> wrote in message news:<cd65v0$roh$>...
    > Hello
    > at the end of my function i return hash:
    > return \%hash;
    >
    > and i want to use that function to receive that hash:
    > my %newhash=MyFunction(); #line 14
    >
    > but i receive error:
    > Reference found where even-sized list expected at line 14.
    >
    > and when i try:
    > my \%newhash=MyFunction(); #line 14
    > i receive syntax error.
    >
    > Why ?
    >
    > Thanx
    > Michal


    Hi,

    You are returning a *reference* to a hash. Thus, you need to do this:
    my $newhash = MyFunction();

    Then, you can access your hash like this:
    print $newhash->{'key'};

    Ofcourse, you can return a 'real' hash ( not a reference to it ) but
    that will copy the entire structure to the calle which is not
    recommended at all.
    Mohammd M. Hussain, Jul 15, 2004
    #6
  7. vertigo

    truptisavant

    Joined:
    Dec 8, 2012
    Messages:
    1
    check out this link

    I just came across this while working on hashes:
    The "return" function can only return a reference to an array. So, I think you would have to do something like:

    $a_hash_table = build_a_hash_table();

    # your hash is now in %$a_hash_table
    # (the hash pointed to by the $a_hash_table scalar)

    # print the hash
    while ( ($k, $v) = each %$a_hash_table ) {
    print "$k => $v\n";
    }

    sub build_a_hash_table
    {
    my(%hash_table);
    #some code to build hash table: "%hash_table" for e.g
    return (\%hash_table);
    }
    hope this helps.
    truptisavant, Dec 8, 2012
    #7
    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. Red Orchid
    Replies:
    3
    Views:
    1,025
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,093
    CBFalconer
    Aug 4, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    796
    Keith Thompson
    Mar 6, 2005
  4. rp
    Replies:
    1
    Views:
    494
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    596
    David A. Black
    Jul 2, 2008
Loading...

Share This Page