in reply to Search and replace text

This assumes that your sample data is a true reflection of it. Ie. Fixed length records of 12 chars + newline.

Update: Removed redundant regex.

#! perl -sw use strict; $/ = \39; ## Read 3 lines as a single record while( <DATA> ) { my $rep = sprintf '%02d', int rand 100; substr $_, 0, 2, $rep; substr $_, 14, 2, $rep; substr $_, 27, 2, $rep; print; } __DATA__ 57KXNWLRZWJ3 057E8bf0D030 057E8bf0D032 58KXNWLRZWKB 058E8bf0D02A 058E8bf0D02C

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Search and replace text
by ikegami (Patriarch) on Jan 26, 2006 at 23:31 UTC
    You're missing
    substr $_, 23, 2, sprintf '%02d', rand 100; substr $_, 36, 2, sprintf '%02d', rand 100;
    and your int is useless.

      I kinda assumed that once the OP had seen the idea of reading the data as fixed length records, he could probably work the rest out for himself.

      As for the int being useless here, point taken. but it's a good habit I think to explicitly truncate the return from rand if you are trying to generate an integer result. In this case it will be autotruncated, but in other situations it will not.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.