returning a hash? - Programmers Heaven

Howdy, Stranger!

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

Categories

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.