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 15:46:38 -0400
Well, I've been using Perl for 15 years, but I can see where you
wouldn't get that from my solution, which as you point out is quite
inefficient, looping much of the way through the string all over again
for each unique number... Your one-pass solution is much better.
I'm curious, though: I don't see what cross-platform operation has to
do with the whole-file slurp. I agree that it's better not to do that
if you can avoid it, but that's just a general memory efficiency
issue. Where does portability come in?
On 5/14/08, Skeeve <email@hidden> wrote:
> How long do you use perl, Mark? It's a bit inefficient what you wrote.
>
> #!/usr/bin/perl -i.bak
> use strict;
> use warnings;
>
> my %numbers; # here our numbers get "counted"
>
> while (<>) {
> # append the count if needed
> # not: It was not defined what should happen after "z"
> # here "aa" will follow
> if (s/^(\d+)(?=\[F\])/$1.($numbers{$1}++||'')/e) {
> # Was it the first occurence?
> if ($numbers{$1} eq 1) {
> # then fix the 1 to become an "a"
> $numbers{$1}= 'a';
> }
> }
> print;
> }
>
> It should work like yours but does not slurp in the whole file (which
> could fail if we are working cross platform) and doesn't us as many
> regexes as yours does.
>
> as a do shell script:
>
> do shell script "perl -i.bak -e " & (quoted form of "use strict;
> use warnings;
>
> my %numbers; # here our numbers get "counted"
>
> while (<>) {
> # append the count if needed
> # not: It was not defined what should happen after "z"
> # here "aa" will follow
> if (s/^(\d+)(?=\[F\])/$1.($numbers{$1}++||'')/e) {
> # Was it the first occurence?
> if ($numbers{$1} eq 1) {
> # then fix the 1 to become an "a"
> $numbers{$1}= 'a';
> }
> }
> print;
> }") & " " & (quoted form of POSIX PATH of inputfile)
> _______________________________________________
> 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
>
--
Sent from Gmail for mobile | mobile.google.com
Mark J. Reed <email@hidden>
_______________________________________________
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