how to get the number of elements of an array on the second level?

[b][red]This message was edited by the Firestorm at 2002-7-4 10:32:42[/red][/b][hr]
hi,
I have a 2-dimensional array.

it is called:
@div_all
and under it there are arrays with diffrent content called @div_one
[blue]
@div_all
[0]@div_one[1]@div_one...
[/blue]
can I get the number of elements in any of the arrays @div_one
with the command:
[red]$#div_all[[green]index of the array I want[/green]][/red]
?????????????

Firestorm
[email protected]
;-)


Comments

  • Try it and tell me what happens, I have no idea. If it doesn't work, the only thing I could think of without seeing the whole script, would be to try it by reference to hat element of the array, then dereference it and get the number of elements, but once again, all this is is a guess :( Like I said, try it the first way and tell me what happens :)
    -----------------------
    [RED]Heero Yuy
    Perl Board Moderator
    Perl Semi-Expert[/RED]

  • [b][red]This message was edited by the Firestorm at 2002-7-4 13:26:13[/red][/b][hr]
    what I suggested is of course wrong otherwise I wouldn't disturb you.
    the error I get is
    syntax error at guest.pl line 74, near "$#div_all["

    if I understood you right you said I can -let's say extract- the needed array and then as usually $#array
    -yes that could be possible!

    sth. like
    [code]
    #loop as many times as there are arrays in @div_all
    for ($j = 0; $j <= $#div_all; $j++)
    {
    @extract =()
    @extract = @div_all[$j]; #so @extract is equal the array div_one at
    #the index1
    $number = $#extract;

    #now I know how many elements I have
    for ($i = 0; $i <= $number; $i++)
    {
    print @div_all[$j][$i];
    }
    }
    [/code]
    that must be it!!!!!!
    I'l have to try......
    good night
    Firestorm
    [email protected]
    ;-)



  • : [b][red]This message was edited by the Firestorm at 2002-7-4 13:26:13[/red][/b][hr]
    : what I suggested is of course wrong otherwise I wouldn't disturb you.
    : the error I get is
    : syntax error at guest.pl line 74, near "$#div_all["
    :
    : if I understood you right you said I can -let's say extract- the needed array and then as usually $#array
    : -yes that could be possible!
    :
    : sth. like
    : [code]
    : #loop as many times as there are arrays in @div_all
    : for ($j = 0; $j <= $#div_all; $j++)
    : {
    : @extract =()
    : @extract = @div_all[$j]; #so @extract is equal the array div_one at
    : #the index1
    : $number = $#extract;
    :
    : #now I know how many elements I have
    : for ($i = 0; $i <= $number; $i++)
    : {
    : print @div_all[$j][$i];
    : }
    : }
    : [/code]
    : that must be it!!!!!!
    : I'l have to try......
    : good night
    : Firestorm
    : [email protected]
    : ;-)
    :
    :
    :
    :

    That might(emphasis on might) work but it is not what I meant :) I meant to do the following. Assign a scalar reference to the elemnt you wanna have. then dereference it and grab the elemtns, and c if that works, i have another idea if this will not work.
    [code]
    $var=$div_all[$j];
    $var2 = $$#var;
    print $var2;
    #or do whatever u wanna do with the value :)
    [/code]

    -----------------------
    [RED]Heero Yuy
    Perl Board Moderator
    Perl Semi-Expert[/RED]

  • If I understand you correctly then I think this is what you are trying to do.
    [code]
    for (my $j = 0; $j <= $#div_all; $j++)
    {
    for (my $i = 0; $i <= $#{$div_all[$j]}; $i++)
    {
    print $div_all[$j][$i],"
    ";
    }
    }
    [/code]
    Or doing it without all the index variables:
    [code]
    my ($row, $elem);
    foreach $row (@div_all) {
    foreach $elem (@$row) {
    print $elem,"
    ";
    }
    }
    [/code]

    austin

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