[Omaha.pm] another hack -- s/\|$key\|/\|$c{$key}\|/;

Jay Hannah jhannah at omnihotels.com
Fri May 27 10:45:08 PDT 2005


Given a survey log like this:

2005-05-12 09:07:42|MONDTN|D10|2|3|3|2|2|4|2|2|1|1|2|2|1|1|4|2|2|3|3|3|2|4|4|4|4|4|3|2|3||
2005-05-12 09:08:54|MONDTN|D16|4|3|4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|3|4|4|4|4|4|4|4|4|4|4|4||
2005-05-12 09:09:56|MONDTN|D16|4|3|4|4|4|3|4|4|3||3|4|3|4|4||4|3|4|4|4|4|4|4|4|3|3|4|3||

Where D10, D16 (etc) are department indicators; and given that you just realized that you wrote the survey thing wrong and need to switch out departments indicators: change all D20's to D14, all D14's to D15, etc.

Solution:

---
my %c = (
  'D20' => 'D14',
  'D14' => 'D15',
  'D15' => 'D16',
  'D16' => 'D17',
  'D17' => 'D18',
  'D18' => 'D19',
  'D19' => 'D20'
);

LINE: while (<>) {
   foreach $key (keys %c) {
      if (/\|$key\|/) {
         s/\|$key\|/\|$c{$key}\|/;
         print;
         next LINE;
      }
   }
   print;
}
---

Bonus points: My first solution just did something like this:

    s/\|D20\|/\|D14\|/;
    s/\|D14\|/\|D15\|/;
    s/\|D15\|/\|D16\|/;
    s/\|D16\|/\|D17\|/;
    etc...

Why was that solution doomed to fail?

j



More information about the Omaha-pm mailing list