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

hi monks i am stuck at some points in my coding. what i want to do is capture the value of the LAYER into an array and print out the max value . problem with my code so far is that when i read the value of @num , all the values are joined together as a string(210) instead of 2 1 0 thus , i am not able to do sorting to get the max value

current result

210
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", ; }

Replies are listed 'Best First'.
Re: how to capture the max value of the array
by haukex (Archbishop) on May 19, 2016 at 10:03 UTC

    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

      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:  <%-{-{-{-<