[comp.lang.perl] Would like to emulate an array of pointers to a structure

okamoto@hpccc.HP.COM (Jeff Okamoto) (03/28/90)

I've got a question that I'm hoping some of you may be able to answer
for me.

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%hpccc@hplabs.hp.com
/___/_____\	..!hplabs!hpccc!okamoto
-----------	(415) 857-6236

john@chinet.chi.il.us (John Mundt) (03/28/90)

In article <12170003@hpccc.HP.COM> okamoto@hpccc.HP.COM (Jeff Okamoto) writes:
> ...
>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?
>

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
john@admctr.chi.il.us *OR* fred@teacha.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/29/90)

In article <12170003@hpccc.HP.COM> okamoto@hpccc.HP.COM (Jeff Okamoto) writes:
: 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).

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