Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Others have commented on the problems with your approach and the way you have coded it. But I gather that your main concern is to make sure that each new person is guaranteed to get a unique password, and you're worried that your current code might not accomplish that.

You are using an SQL-ish database to store names and passwords, so it should be the case that you can (and should) apply a "UNIQUE" constraint on both the name and the password fields -- this way, it you try to insert a row that uses a value already present in the table, the database will reject it, and the insert statement will fail.

This would also mean that you need to work out how to check and handle the return status of the statement execution. Sometimes, the default behavior with DBI is to "die" when something goes wrong, but the way to alter and control this is explained in the DBI man page. (Yes, that man page is long and complicated, but if you're going to use DBI, you need to need to read the man page.)

Of course, you could also take the time to check for the existence of a given user name in the database before you try to insert it -- e.g. (using the table and column names that you seem to have in your database):

my $sql = "select login from login where login like ?"; # (I would have used different names for table and column) my $sth = $dbh->prepare( $sql ); my $newname = "whatever"; $sth->execute( $newname.'%' ); my $likenames = $sth->fetchall_arrayref; # update: remembered that fetchall_arrayref returns AoA, # so added map to get actual field values into grep # (also remembered to include the "execute" step above) if ( grep /^\Q$newname\E$/, map { $$_[0] } @$likenames ) { $newname .= "00"; while ( grep /^\Q$newname\E$/, map { $$_[0] } @$likenames ) { $newname++; } }
After you generate some password string (preferably using the CPAN module suggested above), you can use this sort of logic to check that it's unique and tweak it if necessary.

In reply to Re: making loop in perl script by graff
in thread making loop in perl script by rakie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found