in reply to how to capture the max value of the array

Hi teddy6507,

The code and input you show do not match the behavior you describe - please see How do I post a question effectively? and Short, Self Contained, Correct Example.

When I fix the capitalization of "pin" and "layer", and add the missing while(<>) {...} loop, the code works for me, and I can get the maximum layer number with the code

use List::Util qw/max/; print max(@num), "\n";

I can only guess as to why you get the output "210": maybe you're doing print @num; instead of print "@num"; (see $,), or you've got $" set to the empty string.

In any case, to avoid such ambiguities it's usually much better to use a module like Data::Dumper or Data::Dump to debug your data structures.

use Data::Dump 'pp'; pp \@num; __END__ [2, 1, 0]

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: how to capture the max value of the array
by teddy6507 (Initiate) on May 20, 2016 at 01:15 UTC
    hi Hauke

    sorry on the mismatch. it was a typo on the input source. can you show me please the full code in using the while(<>){...} ? i only pasted a part of the code that lists down operation between the PIN vcc until END vcc.

    input source
    PIN vcc aaaa bbbb Port LAYER m2 ; END CCC DDD Port LAYER m1 ; END EEE FFF Port LAYER m0 ; END END vcc
    my code :
    if (/PIN vcc/../END vcc/) { if(/LAYER\s+m(\S+)/) { push(@num, $1); print "@num" ; }
      if (/PIN vcc/../END vcc/) {
                 if(/LAYER\s+m(\S+)/) {
                   push(@num, $1);
                   print "@num" ;

      Only some schematic suggestions. Basically, follow the advice given above by haukex:

      • Get rid of the  print "@num" ; statement in the if-blocks within your while-loop and replace it with a
            pp \@num;
        statement if you want to see the array grow while the file is being processed.
      • After the while-loop terminates, another  pp \@num; statement can show the final state of the array. Then, get the maximum value with some statement like
            my $max_LAYER_m_number = max @num;
        and you're off to the races.
      If you're still having problems, please show us some brief and complete working code and associated input data and tell us how the output produced falls short of what you need.


      Give a man a fish:  <%-{-{-{-<