in reply to Find and replace by five character string

Some questions: Is it only upper case letters? Does it stop at Z? Is it a specific 5 character string that has four zeroes then an uppercase letter from J to Z at the end? Can there be more than one on a line? Can there be other information on either side? Is it one big line or are there multiple lines in a file? If J is -1 and K is -2, and T is -11, does the string become 000-11 or 00-11?

Replies are listed 'Best First'.
Re^2: Easy find and replace loop. HELP!
by wrml (Novice) on Aug 10, 2005 at 18:33 UTC
    Good questions, I should have clarified.

    the lines are very long and there are a lot of them (these files are very large). I believe the lines are 15,000 characters long and there might be 2,000 of them. there is data on both sides and there can be more than one "0000J" and "0000K" on the same line. The letters go from J to U -12. As for 000-11 or 00-11. they will all always be five characters long. so it is 00-11. I don't know if they are all capitalized, and would like to be prepared if they were not (though I think I know how to make this change in perl). there is certainly no other use for those letters so one need not worry about changing a lowercase that means something else.

    thanks again,

    walter

      Here's one way to do your search and replace. I suggest you look into each element of why this works. Opening the file and looping through the lines should be simple enough (there are many tutorials on that). Note that this one will treat upper and lower case characters the same

      Try this example:
      #!/usr/bin/perl use strict; use warnings; my $string = "ABC0000K0000T12345"; $string =~ s/0000([J-U])/my $x=ord(uc($1))-ord('I');('0'x(4-length($x) +)).'-'.$x/ige; print $string, "\n";


      Update (added links)

      s///
      ord
      length

      tutorial on open