title: How to get the max value from an array in Perl?
body: <p>I have an input source as follows. What I want to do is capture the numeric value on each of the <code>Layer</code> lines into an array and then print out the maximum value.</p>
  
  <h3>input</h3>
  
  <pre class="lang-none prettyprint-override"><code>MACRO cell_1
   size 0.1 by 0.1 ; 
  
   pin a
   (....same topology as pin vcc)
   END a
  
   pin b
   (.....same topology as pin vcc)
   END b
  
   Pin vcc
    aaaa
    bbbb
    Port
     Layer m2 ;
    END
    CCC
    DDD
    Port 
     Layer m1 ;
    END
    EEE
    FFF
    Port
     Layer m0 ;
    END
   END vcc
  
   pin d
   (....same topology as pin vcc)
   END d
  END cell_1
  
  MACRO cell_2
  (repeated)
  END cell_2 
  </code></pre>
  
  <h3>my code:</h3>
  
  <pre class="lang-perl prettyprint-override"><code>foreach my $file ( @files ) {   # @files = multiple path  of abc/def/fgh/cell_lef
  
      open( INFILE, "&lt;$file" ) || die "Can not open stdcell_file\n";
      my @lines = &lt;INFILE&gt;;
      close INFILE;
  
      $init = 1;
      $delimiter =~ /^$/;      # between each MACRO. haven't utilize this yet
  
      foreach (@lines) {
  
          if ( $init ) {
              $path = 1;
              $init = 0;
              @num  = ();
          }
  
          if ( $path ) {
  
              if ( /MACRO\s+(\S+) /) {
                  $cellname = $1; print "$cellname\n";
              }
  
              if ( /SIZE\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
                  $footprint_x = $1;
                  $footprint_y = $3;
                  print "$footprint_x $footprint_y\n";
              }
  
              if ( /PIN vcc/ .. /END vcc/ ) {
  
                  #grab the highest value from layer (m*)
                  #print "max layer = m*"
              }
  
              $init = 1;
          }
      }
  }
  </code></pre>
  
  <h3>intended output</h3>
  
  <pre class="lang-none prettyprint-override"><code>cell_1
  0.1 0.1
  m2
  
  cell_2
  0.2 0.2
  m3
  </code></pre>
  
  <h3>The code I am attempting to use:</h3>
  
  <pre><code>if ( /PIN vcc/../END vcc/ ) {
      if ( /LAYER\s+m(\S+) / ) {
          push(@num, $1);
          print "@num";
      }                               
  }    
  </code></pre>
  
  <p>The problem with my code so far is that when I print the value of <code>@num</code>, all the values are joined together as a string (<code>210</code>) instead of individual elements: <code>2 1 0</code> — so I am not able to do the sorting to get the max value.</p>
  
  <p><strong><em>Update</em></strong>: I am not sure how to integrate the <code>while</code> into my code as I'm using foreach as my main loop</p>