Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

returning a hash?

old_Joeold_Joe Posts: 8Member
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:

[code]## 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;
}[/code]

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

Comments

  • dbrandtdbrandt Posts: 43Member
    Your code is fine, there's just a few typos...
    [code]
    ## calling function
    sub calling_function {

    my %sort_records = &setup_and_sort_records( @rec );

    #do something with sorted records.
    }

    sub setup_[color=Orange]and_[/color]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[color=Orange]{[/color] $key } = $index;
    }
    return %sort_rec;
    }
    [/code]

  • old_Joeold_Joe Posts: 8Member
    : Your code is fine, there's just a few typos...
    : [code]:
    : ## calling function
    : sub calling_function {
    :
    : my %sort_records = &setup_and_sort_records( @rec );
    :
    : #do something with sorted records.
    : }
    :
    : sub setup_[color=Orange]and_[/color]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[color=Orange]{[/color] $key } = $index;
    : }
    : return %sort_rec;
    : }
    : [/code]:
    :
    :
    Thanks!
    (sorry for the type O's I was in a hurry with the example)
    Old Joe
  • JonathanJonathan Posts: 2,914Member
    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.");
Sign In or Register to comment.