Adding a unique user name in a file

Discussion in 'Perl Misc' started by sam, Nov 17, 2004.

  1. sam

    sam Guest

    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
     
    sam, Nov 17, 2004
    #1
    1. Advertising

  2. sam

    Joe Smith Guest

    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
     
    Joe Smith, Nov 17, 2004
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Nov 18, 2004
    #3
  4. 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
     
    Josef Moellers, Nov 18, 2004
    #4
  5. sam

    sam Guest

    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.
     
    sam, Nov 18, 2004
    #5
  6. 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
     
    Josef Moellers, Nov 18, 2004
    #6
  7. 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
     
    Jürgen Exner, Nov 18, 2004
    #7
  8. 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
     
    Jürgen Exner, Nov 18, 2004
    #8
  9. sam

    Anno Siegel Guest

    Jürgen Exner <> 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
     
    Anno Siegel, Nov 19, 2004
    #9
  10. Anno Siegel wrote:
    > Jürgen Exner <> 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
     
    Jürgen Exner, Nov 19, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nalbayo
    Replies:
    2
    Views:
    5,513
    Bruce Barker
    Nov 11, 2005
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,404
    Joe Smith
    Apr 22, 2007
  3. ToshiBoy
    Replies:
    6
    Views:
    853
    ToshiBoy
    Aug 12, 2008
  4. Antonio O''Neal

    user.identity.name returns old AD user name

    Antonio O''Neal, Oct 31, 2008, in forum: ASP .Net Security
    Replies:
    5
    Views:
    2,079
    Antonio O''Neal
    Nov 3, 2008
  5. Token Type
    Replies:
    9
    Views:
    361
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page