Bio_student has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: The biggest value in array 2 dimensin
by ikegami (Patriarch) on Oct 02, 2010 at 04:15 UTC
    I think you mean you want to find the largest.
    my $largest = @table ? $table[0][0] : undef; for my $y (0..$#table) { my $row = $table[$y]; for my $x (0..$#$row) { $largest = $row->[$x] if $largest < $row->[$x]; } }

    Same idea for more dimensions. If the number of dimensions is variable, you can use Algorithm::Loops's NestedLoop.

    Update: Fixed bug in code.

Re: The biggest value in array 2 dimensin
by BrowserUk (Patriarch) on Oct 02, 2010 at 04:46 UTC

    For 2d:

    use List::Util qw[ max maxstr ]; my @data2d = ...; my $max = max( map max( @$_, ), @data2d );

    Or for multi-dimensional:

    use List::Util qw[ max ];; sub maxNd { max( map{ ref() ? maxNd( @$_ ) : $_ } @_ ) };; my @b = ...;; print maxNd( @b );; 9981

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: The biggest value in array 2 dimensin
by TomDLux (Vicar) on Oct 02, 2010 at 04:22 UTC

    If you have an array @a, using it in a scalar context gets the number of elements:

    my @a = ( 3, 1, 4, 1, 5 ); my $N = @a; say $N; # or print "$N\n; before 5.10 my $aref = \@a; $refsize = @$aref; # or @{$aref}; say $refsize;

    For a multidimensional array, you wind up with a reference to the inner array, so use the ref example:

    my @twoD = ( [ 1, 2, 3 ], [ 4, 5, 6 ], ); print "Top level or array has " . scalar @twoD . " elements\n"; for my $row ( @twoD ) { print "row has " . scalar @$row . " elements\n"; }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: The biggest value in array 2 dimensin
by suhailck (Friar) on Oct 02, 2010 at 04:41 UTC
    You can also code this way (for N dimensions)

    perl -le 'my $max="-inf";$array=[[100,20,[32,4]],51,[621,7],[82]]; sub fm { map { $max = ($max < $_) ? $_ : $max } map { ref() ? fm($_) : $_ } @{$_[0]} }; fm($array); print $max ' 621


    Update : Initialised $max to "-inf" upon JavaFan's suggestion.
      Your program has a bug. Had you enabled warnings, you would have found out why. You never initialize $max, which means that in numeric context, $max is treated as if 0. Which means that if all your values are negative, at the end of the procedure, $max is never assigned a new value and still undefined.

      You ought to initialize max to negative infinity:

      my $max = "-inf";
Re: The biggest value in array 2 dimensin
by ww (Archbishop) on Oct 02, 2010 at 12:50 UTC

    - - for removing the original question and replacing it with a 'thank you' note.

    One can hope the consideration process ( will have | quickly ) led to restoration of the original, but since we're here...

    Removing original content (in the manner in this thread) leaves replies less useful than they may have been prior to the editing of the OP: it leaves the replies without context.
    Emending content without explanatory notes of just what was changed creates a context different from that a reply was intended to address. That's misleading, at best.

    In short, don't modify your posts without notice. ( <strike>ing is just one mode of providing notice without changing context. )