freelanceprogrammers.org Forum Index » Perl
Still new to perl
Joined: 07 May 2004
Posts: 7
Still new to perl
I tried the sort function and it`s not working because I know my code
is wrong. Could someone shed some light as to what I`m missing here?
Below is the actual piece of code that attempts to use the sort
function.
open (IN,"ssh $srv_name cat /etc/passwd|") || die "Cannot open file
on $srv_name";
while (<IN>)
{
@uid_sort = $uid = (split /:/) [2];
if (($uid >= 20000) && ($uid <= 30000))
{
@users = sort { $a <=> $b } @uid_sort;
prt_uids();
}
}
The prt_uids is just a subroutine that prints out the uid`s nothing
big. Any help would be appreciated.
Joined: 05 May 2004
Posts: 16
Still new to perl
There are several logic issues, but let me go through the ones I found.
1) I don`t know what you want to do with the "ssh $srv_name cat
/etc/passwd|" line. If you want to open /etc/passwd for parsing just do
it directly:
open (IN,"</etc/passwd") || die "Cannot open passwd file";
2) Don`t do double assignments on commands you don`t even understand.
First split the input line, then assign element [2] to $uid. It is
clearer and it does the job. Also, it does not hurt to chomp the line
first:
while (<IN>) {
chomp; # Removes last newline
@uid_sort = split /:/; # splits line on ":"
$uid = $uid_sort[2]; # gets uid
3) At this point, you have an array @uid_sort with all the elements
from the line you are working on. If what the variable name and future
sort operation tell me, it seems you really want all $uid`s that lie in
the 20000-30000 range to be sorted and printed out. If this is what you
want, this is not what your program is givin you.
After step 1 and 2, you need to see if the $uid is in the range and if
it is, append it to your final array of uids. After the end of the loop
(which works on every line of /etc/passwd), you should then sort your
final uids array and print it out. This you do with
@users = sort { $a <=> $b } @uid_sort;
prt_uids(@users);
but as I tell you, these should be located outside of the main loop,
after processing all /etc/passwd lines.
Think about what you want your program to do before starting to code.
Review your steps and try again. I`m not giving you the complete
answer, but enough info to get it on your own.
Notice however that you need to use a second array to @uid_sort to
split the first line (@elements or whatever name you want, and then
$uid = $elements[2]).
Good luck.
Rex
--- perl_1978 <no_reply@yahoogroups.com> wrote:
> I tried the sort function and it`s not working because I know my code
>
> is wrong. Could someone shed some light as to what I`m missing here?
> Below is the actual piece of code that attempts to use the sort
> function.
>
> open (IN,"ssh $srv_name cat /etc/passwd|") || die "Cannot open file
> on $srv_name";
> while (<IN>)
>
> {
> @uid_sort = $uid = (split /:/) [2];
> if (($uid >= 20000) && ($uid <= 30000))
> {
> @users = sort { $a <=> $b } @uid_sort;
> prt_uids();
> }
> }
>
> The prt_uids is just a subroutine that prints out the uid`s nothing
> big. Any help would be appreciated.
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
>
>
> Yahoo! Groups Links
>
>
>
>
>
Joined: 07 May 2004
Posts: 7
Still new to perl
Actually, step 1 is fine because I`m going to a remote server and not
local.
As for step 2 and 3 - thanks for the advice about my crazy code. I
just needed a little push in the right direction. I will try your
advice.
--- In perl_official@yahoogroups.com, El Rex <rex@b...> wrote:
>
> There are several logic issues, but let me go through the ones I
found.
>
> 1) I don`t know what you want to do with the "ssh $srv_name cat
> /etc/passwd|" line. If you want to open /etc/passwd for parsing
just do
> it directly:
>
> open (IN,"</etc/passwd") || die "Cannot open passwd file";
>
> 2) Don`t do double assignments on commands you don`t even
understand.
> First split the input line, then assign element [2] to $uid. It is
> clearer and it does the job. Also, it does not hurt to chomp the
line
> first:
>
> while (<IN>) {
> chomp; # Removes last newline
> @uid_sort = split /:/; # splits line on ":"
> $uid = $uid_sort[2]; # gets uid
>
> 3) At this point, you have an array @uid_sort with all the elements
> from the line you are working on. If what the variable name and
future
> sort operation tell me, it seems you really want all $uid`s that
lie in
> the 20000-30000 range to be sorted and printed out. If this is what
you
> want, this is not what your program is givin you.
>
> After step 1 and 2, you need to see if the $uid is in the range and
if
> it is, append it to your final array of uids. After the end of the
loop
> (which works on every line of /etc/passwd), you should then sort
your
> final uids array and print it out. This you do with
>
> @users = sort { $a <=> $b } @uid_sort;
> prt_uids(@users);
>
> but as I tell you, these should be located outside of the main loop,
> after processing all /etc/passwd lines.
>
> Think about what you want your program to do before starting to
code.
> Review your steps and try again. I`m not giving you the complete
> answer, but enough info to get it on your own.
>
> Notice however that you need to use a second array to @uid_sort to
> split the first line (@elements or whatever name you want, and then
> $uid = $elements[2]).
>
> Good luck.
> Rex
>
> --- perl_1978 <no_reply@yahoogroups.com> wrote:
> > I tried the sort function and it`s not working because I know my
code
> >
> > is wrong. Could someone shed some light as to what I`m missing
here?
> > Below is the actual piece of code that attempts to use the sort
> > function.
> >
> > open (IN,"ssh $srv_name cat /etc/passwd|") || die "Cannot open
file
> > on $srv_name";
> > while (<IN>)
> >
> > {
> > @uid_sort = $uid = (split /:/) [2];
> > if (($uid >= 20000) && ($uid <= 30000))
> > {
> > @users = sort { $a <=> $b } @uid_sort;
> > prt_uids();
> > }
> > }
> >
> > The prt_uids is just a subroutine that prints out the uid`s
nothing
> > big. Any help would be appreciated.
> >
> >
> >
> >
> > ------------------------ Yahoo! Groups Sponsor
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







