Getting the array from Hash of Array

# I want to get an array from a Hash of Array or
# an Array of Array and put it in @ary.
# ----------------------------------------
# Q1 :What is the way for that ?
# Q2 :Why do I have good resul only with $ary[0][0]
# instead of $ary[0] ??


print "
______Test with Hash______
";

$hoa{lotr} = ["frodo","sam","gandalf","aragorn","legolas"];

@ary = $hoa{lotr};
print "
$ary[0]"; #print ARRAY(0x1a8f14c)
print "
$ary[0][0]"; #print frodo , but why???
print "
$ary[0][1]"; #print sam ???


@ary = @hoa{lotr};
print "
$ary[0]"; #print ARRAY(0x1a8f14c)
print "
$ary[0][0]"; #print frodo
print "
$ary[0][1]"; #print sam


print "
_____Test with Array of Array___________
";

@AoA_Lotr = (["frodo","merry","sam"],
["pippin","gandalf","aragorn"],
["legolas","gimli", "boromir"]);

@ary = @AoA_Lotr[0];
print "
__$ary[0]"; #should print frodo, but I've ARRAY(0x1a85354)
print "
__$ary[0][0]"; #only that print frodo

@ary = $AoA_Lotr[0];
print "
__$ary[0]"; #should print frodo, but I've ARRAY(0x1a85354)
print "
__$ary[0][0]"; #only that print frodo


Thanx for helping :)
[b][size=1][blue]Mitt[red][italic]th'raw'n[/italic][/red]uruodo[/blue][/size][/b]

Comments

  • [blue]print "
    ______Test with Hash______
    ";

    $hoa{lotr} = ["frodo","sam","gandalf","aragorn","legolas"];

    [red]@ary = @{$hoa{lotr}};[/red]
    print "
    $ary[0]"; #prints frodo
    print "
    $ary[0][0]"; #prints nothing

    print "
    _____Test with Array of Array___________
    ";

    @AoA_Lotr = (["frodo","merry","sam"],
    ["pippin","gandalf","aragorn"],
    ["legolas","gimli", "boromir"]);

    [red]@ary = @{$AoA_Lotr[0]};[/red]
    print "
    __$ary[0]"; #prints frodo
    print "
    __$ary[0][0]"; #prints nothing[/blue]

    A1: When you want to pass on an array element from a reference, just put the array funny character @ and bracket the reference element with the curly braces.

    A2: I'm guessing when you gave @ary the element from the reference, it stored it as one element with more elements inside that one. But when you add the @{ .. } you're telling perl you want to add that element as an array.

    Good luck
    --entitledX

  • [red]Thank you very much entitledX ![/red]

    [b][size=1][blue]Mitt[red][italic]th'raw'n[/italic][/red]uruodo[/blue][/size][/b]


Sign In or Register to comment.

Howdy, Stranger!

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

Categories