[comp.lang.perl] SOURCE: lg -list groups

lalonde@mitel ( iccad) (03/14/91)

 I wrote and installed this while ago. Either no one uses it
or it works as advertised. 
 Your comments/critiques are welcome.



NAME
     lg - list groups; diplays users in specified groups or lists
     existing groups

SYNOPSIS
     lg [ groupname ]

DESCRIPTION
     lg with no argument or an argument which  specifies  a  non-
     existant group will list the existing groups.  When given an
     argument describing a valid group the users  in  that  group
     are listed.

     Report   bugs   to   lalonde@mitel.com


#!/usr/bin/perl
$, = ',';		# set output field separator
$\ = "\n";		# set output record separator

{
if( $#ARGV lt 0 ) {
	while(@rec = getgrent) {
			push(@t, @rec[0]);
		}
		undef %mark;
		grep($mark{$_}++,@t);
		@t = sort keys %mark;
		undef %mark;
		printf "There are %s groups:\n",$#t;
		print @t;
	}
else {
	@rec = getgrnam($ARGV[0]); 
	if($#rec >= 0) {
	@t = split(/[ ]+/,@rec[3]);
	while(@pass = getpwent) {
		if( @pass[3] eq @rec[2] ) {
				push(@t, @pass[0]);
		}
	undef %mark;
	grep($mark{$_}++,@t);
	@t = sort keys %mark;
	undef %mark;
	}
	printf "The %d member(s) of group (%s) \"%s\" are: \n", $#t+1,@rec[2],@rec[0];
# If I was a real perl programmer I'd use a "format" wouldn't I?
	for( $j=0; $j <= $#t; $j+=6) { 
		printf "%-8s %-8s %-8s %-8s %-8s %-8s\n",@t[$j],@t[$j+1],@t[$j+2],@t[$j+3],@t[$j+4],@t[$j+5];}
}
else {
	printf "No such group as \"%s\"\n",$ARGV[0];
	exec $0;
}
}
}
-- 
==============================================================================
Terry Lalonde (613)592-2122   | "the war between mainframes and micros is over
Usenet:...!uunet!mitel!lalonde| over and the network has won" 
Internet: lalonde@mitel.com   |                Editors Network Computing 01/91  

merlyn@iwarp.intel.com (Randal L. Schwartz) (03/15/91)

In article <6918@mitel>, lalonde@mitel ( iccad) writes:
[some code]

Argh.  Repeat after me:

$foo or $foo[] or $foo{} gives me a single value;
@foo or @foo[] or @foo{} gives me an array of values.

It just happens that in all the contexts that you used the array refs,
you got away with it.  But someday, you would have gotten burned, and
blamed the language.

And I didn't go over your program with a fine-toothed comb, but the
for loop with all the array-refs had me cringe enough to post this
"how to think in Perl" response...

| # If I was a real perl programmer I'd use a "format" wouldn't I?
| 	for( $j=0; $j <= $#t; $j+=6) { 
| 		printf "%-8s %-8s %-8s %-8s %-8s %-8s\n",@t[$j],@t[$j+1],@t[$j+2],@t[$j+3],@t[$j+4],@t[$j+5];}

##################################################
$j = 0; # column number... 0 is new, 1..6 normally
for (@t) {
	unless ($j++ == 0) {
		if ($j > 6) {
			print "\n"; $j = 1;
		} else {
			print " ";
		}
	}
	printf "%-8s", $_;
}
print "\n";
##################################################

There... and it'll probably run a half-a-millisecond faster too.


# note the number of dots in each (..) below...
print reverse"er,k haclr Perst anotheJu"=~/(...)(.)(....)(.)(.....)(.........)(..)/
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (03/15/91)

: In article <6918@mitel>, lalonde@mitel ( iccad) writes:
: | 	for( $j=0; $j <= $#t; $j+=6) { 
: | 		printf "%-8s %-8s %-8s %-8s %-8s %-8s\n",@t[$j],@t[$j+1],@t[$j+2],@t[$j+3],@t[$j+4],@t[$j+5];}

If I were going to write the loop that way, I'd write the printf as

	printf "%-8s %-8s %-8s %-8s %-8s %-8s\n", @t[$j..$j+5];

In article <1991Mar14.225605.29822@iwarp.intel.com> merlyn@iwarp.intel.com (Randal L. Schwartz) writes:

: ##################################################
: $j = 0; # column number... 0 is new, 1..6 normally
: for (@t) {
: 	unless ($j++ == 0) {
: 		if ($j > 6) {
: 			print "\n"; $j = 1;
: 		} else {
: 			print " ";
: 		}
: 	}
: 	printf "%-8s", $_;
: }
: print "\n";
: ##################################################

Seems a bit long.  How 'bout:

    $j = 0;
    for (@t) { printf "%-8s%s", $_, $j++ < @t && $j % 6 ? ' ' : "\n"; }

or

    $j = 0;
    for (@t) { print $_, $j++ < @t && $j % 6 ? ' ' x (9-length) : "\n"; }

or

    $n = "     \n" x (@t / 6 + 1);
    substr($n,@t-1) = "\n";
    $n = reverse $n;
    for (@t) { printf "%-8s%s", $_, chop($n); }

or

    $n = (' ' x 71 . "\n") x ($#t / 6 + 1);
    $j = 0;
    for (@t) { substr($n,$j,length) = $_; $j += 9; }
    print $n;

or, if you don't mind destroying @t:

    for (@t) {$_ .= ' ' x (8-length);}
    while (@x = splice(@t,0,6)) { print "@x\n"; }

However, like iccad said, maybe you want a format:

    format STDOUT =
    @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<~~
    shift t, shift t, shift t, shift t, shift t, shift t
    .
    write;

That seems quite readable, no?  Redundancy is not always an evil.

Larry

lalonde@mitel ( iccad) (03/18/91)

merlyn@iwarp.intel.com (Randal L. Schwartz) writes:

>Argh.  Repeat after me:

>$foo or $foo[] or $foo{} gives me a single value;
>@foo or @foo[] or @foo{} gives me an array of values.

>It just happens that in all the contexts that you used the array refs,
>you got away with it.  But someday, you would have gotten burned, and
>blamed the language.

Gees pretty fundamental stuff eh? SORRY.
Me blame the language no way I'm sold. 
I'm just a perl rookie gimme a break man.  
Did I get forget to tell you it was my FIRST perl program?
Enough excuses.

Thanks for feedback. 
-- 
==============================================================================
Terry Lalonde (613)592-2122   | "the war between mainframes and micros is over
Usenet:...!uunet!mitel!lalonde| over and the network has won" 
Internet: lalonde@mitel.com   |                Editors Network Computing 01/91