Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!samsung!sol.ctr.columbia.edu!lll-winken!uunet!pmafire!uudell!bigtex!texsun!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: Arrays and me :-( Message-ID: <110866@convex.convex.com> Date: 12 Dec 90 19:19:40 GMT References: <29144@sequoia.execu.com> <484@decvax.decvax.dec.com.UUCP> Sender: usenet@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: usa Organization: CONVEX Software Development, Richardson, TX Lines: 42 In article <484@decvax.decvax.dec.com.UUCP> evans@decvax.DEC.COM writes: :In article <29144@sequoia.execu.com>, painter@sequoia.execu.com (Tom Painter) writes: :|> :|> I'd like to split a passwd file into a multi-dimensional array. :How about the following: Yes, that works. : # read all of the file into the @lines array : open(PASSWD,"/etc/passwd"); : push(@lines,$_) while (); @lines = is faster. : close(PASSWD); : : # print the 5 field of the 50th entry : printf "%s\n", &get_field($lines[50],5); : : # subroutine which decomposes : seperated lines : sub get_field : { local($line,$field) = @_; : local(@fields) = split(/:/,$line); : return(($#fields >= $field) ? $fields[$field] : ""); : } If you deference past the end of the array, you get null anyway. You could actually just make this: sub get_field { (split(/:/,$_[0]))[$_[1]]; } Or else inline it. But those are just speed optimizations, and they don't make all *that* much difference. --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "With a kernel dive, all things are possible, but it sure makes it hard to look at yourself in the mirror the next morning." -me