I'm trying to do some manipulations on the password file. What I'd like
to be able to do is to somehow be able to reference one entire line of
it with a single index value. I know I can do this with an array of
entire password file lines, but I don't want to keep having to split
each line while searching for a particular entry (e.g., login name).
The C equivalent would be an array of pointers to a structure. Is
there an easy (and intuitive!) method for doing what I'm trying to do?
--
\ oo The New Number Who,
\____|\mm Jeff Okamoto
//_//\ \_\ HP Corporate Computing Center
/K-9/ \/_/ okamoto%hp...@hplabs.hp.com
/___/_____\ ..!hplabs!hpccc!okamoto
----------- (415) 857-6236
Try using getpwent() and its associated functions, which allow you to
do just what you are talking about, and which store things in a struct
which has split the different fields out into structure members.
It does it one at a time, but you could create an array of this struct
and fill it from the password file once and play with each line at your
leisure. Alternatively, there is a reset function to these functions
setpwent() which rewinds the file and starts again. Don't forget to
call endpwent() when you are all done.
--
---------------------
John Mundt Teachers' Aide, Inc. P.O. Box 1666 Highland Park, IL
jo...@admctr.chi.il.us *OR* fr...@teacha.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem
Whenever you find yourself saying things like "searching for a particular
entry", it probably means you should invert the database with one or more
associative arrays.
: The C equivalent would be an array of pointers to a structure. Is
: there an easy (and intuitive!) method for doing what I'm trying to do?
If you want to program in C, program in C. It's a nice language. I use
it occasionally... :-)
The closest thing would be parallel arrays, but again, that's probably not what
you wanna do. You could generate an array of arrays using the * notation:
$sym = 'X000';
$i = 0;
while (<PASSWD>) {
chop;
$pw[$i++] = *entry = $sym++;
@entry = split(/:/);
}
# later...
*entry = $pw[$somenum];
if (@entry[0] == $login) { ...
But it's probably more "intuitive" to split each time. Split on a single
character is fairly efficient.
But don't do it that way. Use associative arrays. Or just search with
a pattern on the whole line--Perl will do it very efficiently if there
are any constant strings in the pattern.
Perl isn't about nested data structures. It prefers flat data structures.
Perl is more like a chainsaw than a jig saw. Or think of Perl as a bigger
hammer. It lets you treat everything like a nail, and get away with it most
of the time. But sometimes not.
Larry