A co-worker supports a POS system. At month end, system A creates some new XML, and sends them to System B that processes them into a billing system.

Problem is that System B only handles a 10 character OPERATORID value, and System A will create 12 character OPERATORID's. So hundred's of these are rejected. cute, eh?

So we get System A to only send 10 chars, right? wrong.

For whatever reason, this co-worker decided to HAND MASSAGE the files and using notepad to truncate all the rogue >10 chars strings.

Last month the system spit out over 400 of these rejections (business is picking up!).

while watching this co-worker hand-massage these files, I decided to write up a quick script to do it for him.

It's not going to win any prizes in a perl contest, but it sure solved a huge problem for my co-worker.

The file works against one file at a time (it started as an attempt at a one-liner, but time was of the essence and I was not clever enough to do it that way). I wrote a second perl script to build a big .BAT file from the directory listing the files resided in. The batch file executes this script against each file.

I thought I would just share the story, but I will be brave and include the code, such as it is.

#!perl.exe # invoke as follows: # perl -p -i.BAK posfix.pl FILENAMETOPROCESS # if ( /OPERATORID/ ) { $_ =~ s/<OPERATORID>//; $_=~ s/<\/OPERATORID>//; $_=substr($_,0,10); $_ = "<OPERATORID>". $_ . "</OPERATORID>\n"; }

Replies are listed 'Best First'.
RE: Quick solutions to easy problems
by tilly (Archbishop) on Aug 18, 2000 at 00:28 UTC
    You were very close to the one-liner. :-)
    perl -i.BAK -pe 's!(<OPERATORID>)(.*)(</OPERATORID>)!$1 . substr($2, 0 +, 10) . $3!e' FILE1 FILE2 ...
      A very cool feature of perl, I call it the Pie Bake '-pi.bak -e' solution :) to remember the switches easily.

      I love this place. I learn something useful everytime I log in.

      thank you monks!

RE: Quick solutions to easy problems
by merlyn (Sage) on Aug 18, 2000 at 06:30 UTC
RE: Quick solutions to easy problems
by tenatious (Beadle) on Aug 21, 2000 at 05:44 UTC
    I'm not going to write a one liner. I'm going to do it slower and with modules that I like:
    use XML::Simple; opendir DIRECTORYOFNAUGHTYFILES, "/directory/path"; @xfiles = grep !/\.\.?/, readdir DIRECTORYOFNAUGHTYFILES; @xfiles = grep /\.xml$/, @xfiles; for $xfile (@xfiles) { $xfile="/directory/path/$xfile"; $xml = XMLin($xfile); $xml->{OPERATORID} = substr($xml->{OPERATORID},0,10); $newxml = XMLout($xml); open XFILE, ">$xfile"; print XFILE $newxml; }
    The only trouble with this, is that it will probably change the original XML file structure a bit. You'll probably want to write a write_xml sub or something to overcome this. Another problem is that it does every file in the directory... you may not want that to happen. Another problem is that you might just want to do one file at a time. And I'm sure there are other problems. I like to use XML::Simple for just about everything, and when a man first gets a hammer, he thinks everything is a nail.
RE: Quick solutions to easy problems
by Anonymous Monk on Oct 08, 2000 at 07:38 UTC
    What happens when the truncated OPERATORIDs are not unique? You'd be much better off patching system B to accept longer ones. Failing that, have your perl script keep track of all IDs it's seen (original and truncated versions) and generate unique ones when needed.