Rob Dixon wrote:

> Kenton Brede wrote:
> > __DATA__
> > user1:x:700:101:user1:/dev/null:/bin/false
> > user2:x:706:101:user2:/dev/null:/bin/false
> > user3:x:707:101:user3:/dev/null:/bin/false
> > user4:x:708:101:user4:/dev/null:/bin/false
>
> Hi Kenton.
>
> How does this look?
>
>   sub get_uid {
>     my %list;
>     @list{map {(split /:/)[2]} <DATA>} = ();
>     return (grep {not exists $list{$_}} 700 .. 799)[0];
>   }
>
> More readably, this does almost the same thing:

I think there is a happy medium that harnesses the power of Perl's aggregate
tools without the obscurity of the above:
Greetings! E:\d_drive\perlStuff\JPEG>perl -w
my @uid_list = map {(split /:/)[2]} (<DATA>);
my %uid_list;
@[EMAIL PROTECTED] = ([EMAIL PROTECTED]);
my $free_number = 700;
while ($free_number < 800) {
   last unless $uid_list{$free_number};
   $free_number++;
}
print "$free_number\n";

__DATA__
user1:x:700:101:user1:/dev/null:/bin/false
user2:x:706:101:user2:/dev/null:/bin/false
user3:x:707:101:user3:/dev/null:/bin/false
user4:x:708:101:user4:/dev/null:/bin/false
^Z
701

You can see the reason for my preference by adding the diagnostic line:
print "$_: $uid_list{$_}\n" for keys %uid_list;
at the end of each script.

Of course, it doesn't really matter in the given context, but somehow I don't
like the idea of hash elements
hanging in that limbo of "existing but undefined".  I also think that exploding
the view of that one-liner better
demonstrates each of its constituent features.

Of course, it might better demonstrate the process of assignment to a hash slice
to vary the names a bit
and call the hash a hash:
my %uid_hash;
@[EMAIL PROTECTED] = ([EMAIL PROTECTED]);
...

>   sub get_uid {
>
>     my %list;
>
>     while(<DATA>) {
>       my $uid = (split /:/)[2];
>       $list{$uid}++;
>     }
>
>     foreach my $uid (700 .. 799) {
>       return $uid unless exists $list{$uid};
>     }
>
>     return undef;
>   }

This is definitely the most straightforward approach.

>
>
> HTH,
>
> Rob

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to