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. Advertisements

  2. No, you don't. You are returning a reference.
    Right. You cannot assign a scalar value to a hash variable.
    Because this is not Perl syntax.

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

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

  3. vertigo

    vertigo Guest

    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. 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


    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. 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
    Likes Received:
    0
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.