It was Friday, June 13, 2003 when [EMAIL PROTECTED] took the soap box, saying:
: Can someone tell me am I going about this the right way?
: 
: my @user=(split(/:/,(@passwd=(split(/\n/,`cat /etc/passwd`)))[0]));
: for($i=0;$i <= $#passwd ; $i++)
: {
: print "$user[$i]\n";
: print "$passwd[$i]\n";
: }

Whoa.  That looks like complicated code.  I think we can clean it up.
Your first attempt with a while loop was much closer.  And you
probably shouldn't be trying to juggle data between two arrays like
that.  Why don't we go back to your original attept.

I'm going to assume that order isn't important to you.

  open( PASSWD, "/etc/passwd" ) || die $!;
  my %records;
  while (<PASSWD>) {
    my($user, $pass) = split( /:/, $_ );
    $records{$user} = $pass;
  }
  close( PASSWD );

That was easy, and I know from your first exapmle that you understand
it pretty well too.  If order is important to you, you want the list
to stay in the same order as the passwd file, you can change this
arround a bit and use a list of hashes.

  my @records;
  while (<PASSWD>) {
    my($user, $pass) = split( /:/, $_ );
    push @records, {
      user => $user,
      pass => $pass,
    };
  }

Enjoy!

  Casey West

-- 
"Louis Pasteur's theory of germs is ridiculous fiction".
 -- Pierre Pachet, Professor of Physiology at Toulouse, 1872


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to