Re: Find duplicate numbers and replace
Re: Find duplicate numbers and replace
- Subject: Re: Find duplicate numbers and replace
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 14 May 2008 13:56:37 -0400
For the interested, here's a standalone, fully-commented version of
the Perl script from my reply to Script2.
#!/usr/bin/perl -i.bak
# the -i.bak option ("(-i)n-place edit with backup named .bak") causes the
# script to make a copy of its input file, with the extension .bak, and then
# replace the original file with the output of the script.
# undefining the input record delimiter ($/) causes a subsequent request
# for input to return the entire contents of the file as one big string
undef $/;
# <> reads from input; we store the big string in $_, which is a convenient
# place to use because it's the default target for pattern matching.
$_ = <>;
# Within $_, look for sequences of one or more (+) digits (\d)
followed by "[F]".
# The parentheses around the \d+ indicate that the particular digits
found are captured
# in a variable ($1) for later use. Square brackets have special meaning in
# regular expressions and have to be escaped with backslashes to match
# literally. The /g causes the search to pick up where it left off every
# time it comes back to the top of the loop, rather than starting over
# at the beginning of the string. The x means the whitespace in the regular
# expression is ignored, which lets it be written more clearly.
while (/ ( \d+ ) \[F\] /gx)
{
# Save a copy of the number we found in a more stable place
# ($1 gets overwritten when we do the s///g below)
my $n = $1;
# The string to append to the next occurrence of the same number
my $a = "a";
# We don't want to touch anything before the current position in
# the string. pos() points just past the last match, so a substring
# starting there is what we want to operate on
substr($_, pos) =~
# look for the same number ($n), either at the beginning of
the string (^)
# or after a non-digit (this condition keeps "1" from matching on "11",
# "231", etc), followed by [F]. Replace it with the same things but
# with the value of $a inserted between the number and the [F]. Then
# increment $a (that's the "++"), which changes it to the next letter
# in the alphabet.
# Normally the replacement part of the s/// operator is
treated as just a
# string, but the /e lets us use an arbitrary Perl expression instead.
# /g in this case means replace every occurrence, not just the first,
# and /x again allows the regular expression to be spread out for
# clarity.
s/ ( ^ | \D ) $n \[F\] / $1 . $n . $a++ ."[F]" /egx;
}
# After we've done all our replacements, print out the modified string
print;
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden