Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
returning a hash? Posted by old_Joe on 17 Jan 2008 at 11:34 AM
I'm having trouble figuring out how to return a hash I create in a sub function and if I should treat it like a reference when it's returned to my calling function. Here's what I have:

## calling function
sub calling_function {

    my %sort_records = &setup_and_sort_records( \@rec );

    #do something with sorted records.
}

sub setup_sort_rec {

    my $rec = shift;
    my %sort_rec;
    my $key = "";

    for ( my $index = 0; $index < @$rec; $index++ ) {

          $key =  $$rec[ $index ]{id} . '_';
          $key .= $$rec[ $index ]{person} . '_';
          $key .= $$rec[ $index ]{number} . '_';
          $key .= $$rec[ $index ]{date} . '_';
          $key .= $$rec[ $index ]{time};

          $sort_rec( $key } = $index;
    }
    return %sort_rec;
}


If you see anything wrong here I would appreciate any help.
Thanks in advance,
Old Joe

Report
Re: returning a hash? Posted by dbrandt on 17 Jan 2008 at 10:33 PM
Your code is fine, there's just a few typos...
## calling function
sub calling_function {

    my %sort_records = &setup_and_sort_records( \@rec );

    #do something with sorted records.
}

sub setup_and_sort_rec {

    my $rec = shift;
    my %sort_rec;
    my $key = "";

    for ( my $index = 0; $index < @$rec; $index++ ) {

          $key =  $$rec[ $index ]{id} . '_';
          $key .= $$rec[ $index ]{person} . '_';
          $key .= $$rec[ $index ]{number} . '_';
          $key .= $$rec[ $index ]{date} . '_';
          $key .= $$rec[ $index ]{time};

          $sort_rec{ $key } = $index;
    }
    return %sort_rec;
}


Report
Re: returning a hash? Posted by old_Joe on 18 Jan 2008 at 8:01 AM
: Your code is fine, there's just a few typos...
:
: 
: ## calling function
: sub calling_function {
: 
:     my %sort_records = &setup_and_sort_records( \@rec );
: 
:     #do something with sorted records.
: }
: 
: sub setup_and_sort_rec {
: 
:     my $rec = shift;
:     my %sort_rec;
:     my $key = "";
: 
:     for ( my $index = 0; $index < @$rec; $index++ ) {
: 
:           $key =  $$rec[ $index ]{id} . '_';
:           $key .= $$rec[ $index ]{person} . '_';
:           $key .= $$rec[ $index ]{number} . '_';
:           $key .= $$rec[ $index ]{date} . '_';
:           $key .= $$rec[ $index ]{time};
: 
:           $sort_rec{ $key } = $index;
:     }
:     return %sort_rec;
: }
: 
:
:
:
Thanks!
(sorry for the type O's I was in a hurry with the example)
Old Joe
Report
Re: returning a hash? Posted by Jonathan on 24 Jan 2008 at 1:37 AM
Hi,

So does it work now? I'd probably write:

$$rec[ $index ]

As

$rec->[ $index ]

Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.