Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Search for dupe && append char if found

by BlaisePascal (Monk)
on Aug 22, 2000 at 12:09 UTC ( [id://28981]=note: print w/replies, xml ) Need Help??


in reply to Search for dupe && append char if found

I'm not sure what you mean by "how will the program know which string to modify?". I assume the answer to that would be "by following the algorithm, and modifying the string the algorithm says to".

One of the standard tricks for detecting duplicates is by using a hash:

# Read passwd files from @ARGV, outputting one record per # username my %users; while (<>) { $user = split ":",$_,1; next if exists $users{$user}; print; $users{$user} = 1; }
The first time username foo is found, $users{'foo'} does not exist, so the record is printed out, and $users{'foo'} is set to 1. The second time username foo is found, $users{'foo'} is now 1, so the record is skipped.

You didn't want to skip records, but rather rekey the records, so that the duplicates have different keys -- you wanted your outputted flat-file to have unique keys. Doing that with the passwd example:

# Read passwd files from @ARGV, outputting one record per # username. Duplicate usernames are renamed to be unique my %users; while (<>) { $user = split ":",$_,1; # next if exists $users{$user}; $_ = "a$_" , redo if exists $users{$user}; print; $users{$user} = 1; }
The $_ .= "a" , redo statement that replaces the next appends "a" to the incoming line and then reprocesses it. I decided to reprocess it rather than just add "a" because if I had three user "foo" in the input files, I wouldn't want to get users "foo", "afoo", and "afoo", I'd want "foo", "afoo", "aafoo".

I hope this helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found