Wednesday, February 3, 2010

Perl tips - array of records, command line params, STDIN/STDOUT handles

set of records or 2D array
  • To store a set of records, use  my @data; $data[i]->[j] = $someval; (ith record, jth field). This basically works similar to a 2-D array except that each row can have different size.
  • To sort the records based on column j, do @sorteddata = sort {$a->[j] <=> $b->[j]} @data;.
Command line parameters
  • The format for specifying parameters is cmd OPT1=sth OPT2=sth ...
  • Use while(@ARGV){ if(m{OPT1=}){ use $'; }elsif(m{OPT2}){...}}.
Others
  • Use $opstream = *STDOUT; (note the '*') to pass (both read and write) file handles around and use them via variables (e.g. print $opstream "qwe";).
  • To get floor of a number, do use POSIX; $val = POSIX::floor($num);.

No comments:

Post a Comment