The "best" way depends on many factors and frankly speaking in your case I don't think it matters much. The passwd file is not too big. I am not going to code it for you I'll give you some hints instead.
You’re not really clear on the type of user interface needed. I’ll assume the command line will be fine.
The Unix passwd file is structured, it consists of records, separated by a colon. I assume a unique key for each record in the passwd file, i.e. the first column.
Apparently the user inputs a name for which you want to search the file?
- Open the file for reading
- Parse the file
Each line is a record, use for example
For each line:
@fields = split(/:/, $record);
to get the individual fields. Store the stuff in some data structure (e.g. Array of Hashes, Hash of Hashes).
- Close the file.
- Look for the fields you’re interested in, e.g. first name, second name.
- If it matches store the key in a list, say @matching_users
- After this you have a list with candidates, output the first 5 (or less) to the screen.
- Read the user input to select a user.
- Output the selected user to the screen.
You might want to add some error handling for incorrect user input.
You might want to combine/leave out steps to improve the solution.
| [reply] |
and the option of an SQL database is out for me
No it isn't :) if you use a database-in-a-file, like DBD::SQLite,
or DBD::AnyData
| [reply] |
I can only use core modules, and perl version is 5.003. Yes, I know, please no lectures though...
| [reply] |
Have you looked at String::Approx? If you can split the name into tokens, this would probably suffice. You might have to be creative if you have a case where names run together:
Cowboy P Neal
cowboyneal
It still could probably be made to work even in this case. | [reply] |