Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Adding a unique user name in a file

Reply
Thread Tools

Adding a unique user name in a file

 
 
sam
Guest
Posts: n/a
 
      11-17-2004
Hi,

Is there any perl module I can use to read in a list of user names from
a file and do a binary search on the list base on the user name, if the
user name is not found, add this user name to the file?

Thanks
Sam
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      11-17-2004
sam wrote:
> Is there any perl module I can use to read in a list of user names from
> a file and do a binary search on the list base on the user name, if the
> user name is not found, add this user name to the file?


Why do a binary search? You can accomplish the same thing by
tying a hash to a file. Then use ordinary hash operations to test
for the existance of a given key. Or use
$file_based_hash_of_names{$username} = 1;
unconditionally to add it to the list.

-Joe
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-18-2004
sam wrote:
> Is there any perl module I can use to read in a list of user names
> from a file and do a binary search on the list base on the user name,
> if the user name is not found, add this user name to the file?


You don't need a module for this. It's probably 15-20 lines of code only.

Also, why do a binary search? If you read the user names into a hash then
you get an O(1) access instead of a O(log(n)) as with a binary search.

jue


 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-18-2004
Jürgen Exner wrote:
> sam wrote:
>
>>Is there any perl module I can use to read in a list of user names
>>from a file and do a binary search on the list base on the user name,
>>if the user name is not found, add this user name to the file?

>
>
> You don't need a module for this. It's probably 15-20 lines of code only.
>
> Also, why do a binary search? If you read the user names into a hash then
> you get an O(1) access instead of a O(log(n)) as with a binary search.


Why the repeated reference to a hash? If I was given a user name to look
up in a file, I'd step through the file entry-by-entry. If I find the
user, I'll process his entry, close the file and be done with it. If I
didn't find him/her, I'll have reached the end of file and, given the
proper open mode, append a record for the (new) user. No hash, no fancy
legwork, just plain coding.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
sam
Guest
Posts: n/a
 
      11-18-2004
Josef Moellers wrote:

> Jürgen Exner wrote:
>
>> sam wrote:
>>
>>> Is there any perl module I can use to read in a list of user names
>>> from a file and do a binary search on the list base on the user name,
>>> if the user name is not found, add this user name to the file?

>>
>>
>>
>> You don't need a module for this. It's probably 15-20 lines of code only.
>>
>> Also, why do a binary search? If you read the user names into a hash
>> then you get an O(1) access instead of a O(log(n)) as with a binary
>> search.

>
>
> Why the repeated reference to a hash? If I was given a user name to look
> up in a file, I'd step through the file entry-by-entry. If I find the
> user, I'll process his entry, close the file and be done with it. If I
> didn't find him/her, I'll have reached the end of file and, given the
> proper open mode, append a record for the (new) user. No hash, no fancy
> legwork, just plain coding.
>

Right, this is for small number of users.
To deal with large number of users, one would need to load all user
records into memory when the system/server start up, and use hash/tree
to speed up the search rather than using linear search.

Sam.
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-18-2004
sam wrote:
> Josef Moellers wrote:
>
>> Jürgen Exner wrote:
>>
>>> sam wrote:
>>>
>>>> Is there any perl module I can use to read in a list of user names
>>>> from a file and do a binary search on the list base on the user name,
>>>> if the user name is not found, add this user name to the file?
>>>
>>>
>>>
>>>
>>> You don't need a module for this. It's probably 15-20 lines of code
>>> only.
>>>
>>> Also, why do a binary search? If you read the user names into a hash
>>> then you get an O(1) access instead of a O(log(n)) as with a binary
>>> search.

>>
>>
>>
>> Why the repeated reference to a hash? If I was given a user name to
>> look up in a file, I'd step through the file entry-by-entry. If I find
>> the user, I'll process his entry, close the file and be done with it.
>> If I didn't find him/her, I'll have reached the end of file and, given
>> the proper open mode, append a record for the (new) user. No hash, no
>> fancy legwork, just plain coding.
>>

> Right, this is for small number of users.
> To deal with large number of users, one would need to load all user
> records into memory when the system/server start up, and use hash/tree
> to speed up the search rather than using linear search.


You asked to "search on the list base on _the_user_name_", so I deduced
that you were having a go at the database with a single user name (e.g.
when adding a user to a system). If you need to batch-process several
user names in one go, then, obviously, a hash would be a suitable data
structure.

The size of the database would also play a role: loading a huge database
into memory would require a lot of memory (which is cheap these days),
reading one record at a time would not, so that would involve a
classical time/space-tradeoff.

The next step, then, would be to consider converting your list of user
names into a database (eg using the DBM modules) and then working on
that rather than the more unstructured list of names.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-18-2004
Josef Moellers wrote:
> Jürgen Exner wrote:
>> sam wrote:
>>
>>> Is there any perl module I can use to read in a list of user names
>>> from a file and do a binary search on the list base on the user
>>> name, if the user name is not found, add this user name to the file?

>>
>>
>> You don't need a module for this. It's probably 15-20 lines of code
>> only. Also, why do a binary search? If you read the user names into a
>> hash
>> then you get an O(1) access instead of a O(log(n)) as with a binary
>> search.

>
> Why the repeated reference to a hash? If I was given a user name to
> look up in a file, I'd step through the file entry-by-entry. If I
> find the user, I'll process his entry, close the file and be done
> with it. If I didn't find him/her, I'll have reached the end of file
> and, given the proper open mode, append a record for the (new) user.
> No hash, no fancy legwork, just plain coding.


True! Sometimes the simplest methods are the best.
Thanks for reminding us that you have to read the file completely anyway.
Then you can just as well do the search at the same time.

jue


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-18-2004
sam wrote:
>>> sam wrote:
>>>
>>>> Is there any perl module I can use to read in a list of user names
>>>> from a file and do a binary search on the list base on the user
>>>> name, if the user name is not found, add this user name to the
>>>> file?

>>
>> [Just check for the name while reading the file]
>>

> Right, this is for small number of users.
> To deal with large number of users, one would need to load all user
> records into memory when the system/server start up, and use hash/tree
> to speed up the search rather than using linear search.


You realize that the second description is a totally different problem then
the first, do you?

jue


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-19-2004
Jürgen Exner <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> sam wrote:
> >>> sam wrote:
> >>>
> >>>> Is there any perl module I can use to read in a list of user names
> >>>> from a file and do a binary search on the list base on the user
> >>>> name, if the user name is not found, add this user name to the
> >>>> file?
> >>
> >> [Just check for the name while reading the file]
> >>

> > Right, this is for small number of users.
> > To deal with large number of users, one would need to load all user
> > records into memory when the system/server start up, and use hash/tree
> > to speed up the search rather than using linear search.

>
> You realize that the second description is a totally different problem then
> the first, do you?


Well, OP didn't specify the number of *which* users is going to be small
or large. If it's a large number of users being *added*, slurping the
existing users into a hash looks reasonable. If it's adding *to* a large
base of existing users, building the hash for each new user would be a
waste of time.

The little remark about loading things into memory "when the system
starts up" seems to suggest running the process as a server. That
would indeed be an entirely different problem and open up a whole
slew of questions. I'm not going there for now.

Anno
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-19-2004
Anno Siegel wrote:
> Jürgen Exner <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> sam wrote:
>>>>> sam wrote:
>>>>>
>>>>>> Is there any perl module I can use to read in a list of user
>>>>>> names from a file and do a binary search on the list base on the
>>>>>> user name, if the user name is not found, add this user name to
>>>>>> the file?
>>>>
>>>> [Just check for the name while reading the file]
>>>>
>>> Right, this is for small number of users.
>>> To deal with large number of users, one would need to load all user
>>> records into memory when the system/server start up, and use
>>> hash/tree to speed up the search rather than using linear search.

>>
>> You realize that the second description is a totally different
>> problem then the first, do you?

>
> Well, OP didn't specify the number of *which* users is going to be
> small or large. If it's a large number of users being *added*,
> slurping the existing users into a hash looks reasonable.
> If it's
> adding *to* a large base of existing users, building the hash for
> each new user would be a waste of time.


True, but I have a gut feeling this is not the culprit

> The little remark about loading things into memory "when the system
> starts up" seems to suggest running the process as a server. That
> would indeed be an entirely different problem and open up a whole
> slew of questions. I'm not going there for now.


That's what I was alluding at.

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Will adding user avoid unique program drivers from loading at Admin startup? vidmaven8139021@nohyahoospam.org Computer Support 0 09-06-2006 09:54 PM
HttpContext.Current.User.Identity.Name AND Context.User.Identity.Name; nalbayo ASP .Net 2 11-11-2005 11:12 PM



Advertisments