Path: utzoo!attcan!uunet!samsung!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Would like to emulate an array of pointers to a structure Message-ID: <7577@jpl-devvax.JPL.NASA.GOV> Date: 28 Mar 90 17:52:57 GMT References: <12170003@hpccc.HP.COM> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 46 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 () { 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