Howdy, Stranger!

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

Categories

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

FirestormFirestorm Member Posts: 162
[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
fgretz@gmx.de
;-)


Comments

  • heeroyuyheeroyuy Member Posts: 125
    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]

  • FirestormFirestorm Member Posts: 162
    [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
    fgretz@gmx.de
    ;-)



  • heeroyuyheeroyuy Member Posts: 125
    : [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
    : fgretz@gmx.de
    : ;-)
    :
    :
    :
    :

    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]

  • justice41justice41 Member Posts: 1
    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

  • Andre YoungAndre Young USAMember Posts: 0

    _______ / http://forcoder.org / free video tutorials and ebooks about < PHP MATLAB Delphi Ruby C++ Scratch Visual Basic .NET JavaScript Assembly Python C Visual Basic C# R Swift PL/SQL Perl Java Objective-C Go D LabVIEW Dart Hack Julia Fortran Ada Apex Crystal Clojure FoxPro F# Kotlin Rust ML Lisp Lua Transact-SQL ABAP Alice Awk Scala VBScript COBOL Bash Erlang Prolog SAS Logo Scheme /> ______

Sign In or Register to comment.