Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Code Smarter

by japhy (Canon)
on Nov 19, 2000 at 02:18 UTC ( [id://42374] : perlmeditation . print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    @small = grep { $_->size < 100 } sort $query->all_records;
    # vs.
    @small = sort grep { $_->size < 100 } $query->all_records;
    
  2. or download this
    ($short) = $desc =~ /^(.{0,100})/;
      # breaks on embedded newlines, and is better as
    $short = substr($desc, 0, 100);
    
  3. or download this
    $name = substr($rec,  0, 20);
    $age  = substr($rec, 20,  5);
    $job  = substr($rec, 25, 25);
      # repeated calls to substr() better as unpack()
    ($name,$age,$job) = unpack 'A20 A5 A25', $rec;
    
  4. or download this
    if ($str =~ /jeff/) { ... }
      # why not
    if (index($str,'jeff') > -1) { ... }
      # or if you know that if it's there, it's toward the end:
    if (rindex($str,'jeff') > -1) { ... }
    
  5. or download this
    # (on average)
    s/[abc]*//g;  # is FAR slower than
    s/[abc]//g;   # is slower than
    s/[abc]+//g;  # which is slower than
    tr/abc//d;