The central error that leads to the behaviour of your code are the following two lines:

... foreach(@arr) { @IDINFO = split(/:/,"$arr[$_]"); ...

You should see lots of warnings by Perl, because $arr[$_] will always be $arr[0], because $_ is not what you might think it is.

As a first approach to finding such errors yourself, I would suggest printing $_ and $arr[ $_ ] to make sure that what you see is what you think there should be.

As a first fix, use $_ instead of $arr[ $_ ].

In general, there are many things that you can improve in your program to make it more resilient and not rely on features that were superseded by features introduced in Perl 5:

$HAN = "handle"; $FILE = "passwd.txt"; open("$HAN", "$FILE")||die "Error opening file: $!\n";

Please do not use global filehandles. Whatever textbook promotes such use, it is hopefully very old, as this is a technique used in Perl 4. Using this technique relies on the idea that nobody nowhere in your program will ever use a filehandle with a similar name. The Perl5 way is to simply use lexical filehandles:

open my $HAN, $FILE or die "Error opening file '$FILE': $!";

Now, your file iteration logic is also problematic because it mixes two approaches:

while(<$HAN>) { @arr = <$HAN>; }

The while(<$HAN>) { reads one line from $HAN and stores it in $_.

The @arr= <$HAN> reads all remaining lines from $HAN and stores them in @arr.

You should use either one of the two idioms but not mix them.

... print "UID is: @IDINFO[2] \n"; print "GID is: @IDINFO[3] \n"; ...

The common way to write these is to use $ instead of @:

print "UID is: $IDINFO[2] \n"; print "GID is: $IDINFO[3] \n";

In reply to Re: traverse through passwd file, and split fields to extract UID/GID by Corion
in thread traverse through passwd file, and split fields to extract UID/GID by intoperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.