CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Incrementing by one?

 



Tim Mousel
Deleted

Mar 22, 2001, 10:05 PM

Post #1 of 5 (461 views)
Incrementing by one? Can't Post

Hi,

Here is an example of a text file:

982807113:2209
982807113:2210
982807113:2211
1002679113:2212
1003975113:2213

There are 1000 usernames:passwords in this file and many of the usernames are the same. I need to make each username unique by incrementing each one by one so the end result would be like this:

982807113:2209
982807114:2210
982807115:2211
1002679113:2212
1003975114:2213

How do I do that?

Thanks,

Tim




Jasmine
Administrator / Moderator

Mar 23, 2001, 12:01 AM

Post #2 of 5 (460 views)
Re: Incrementing by one? [In reply to] Can't Post

Uncommented version

Code
#!/usr/bin/perl -w 

my %hash;

open ( DATA, "<users.txt" ) or die "Can't open data file $!\n";
while ( <DATA> ){
chomp;

my ( $user, $pw ) = split( /:/ );

unless ( exists $hash{$user} ){
$hash{$user} = $pw if $user;
}
else {
EXISTS : while ( each %hash ){
$user++;
redo EXISTS if exists $hash{$user};
$hash{$user} = $pw;
last EXISTS;
}
}
}
close DATA or die "Can't close data file $!\n";

for my $user (sort { $a <=> $b } keys %hash ){
print "User: $user Password: $hash{$user} \n";
}

Commented version:


Code
#!/usr/bin/perl -w 

my %hash;

open ( DATA, "<users.txt" ) or die "Can't open data file $!\n";
while ( <DATA> ){
chomp;

# first, extract username/pw from line

my ( $user, $pw ) = split( /:/ );


# now, if the username hasn't already been read, toss it into the hash

unless ( exists $hash{$user} ){
$hash{$user} = $pw if $user;
}


else {
# this part will only be run if it's a duplicate username.
# we can't just increment it because we don't want to assume that
# simply incrementing it will make it unique. So we're going to
# loop through the keys of the hash to make sure it's not already
# defined.

# I like to use loop labels. This makes it very easy to
# remember what's going on when you have to review the code 3
# months later, or if you're nesting loops. Also note that
# we're using the redo and last, which makes labels necessary
# if loops are being nested.


EXISTS : while ( each %hash ){

# start off the loop with incrementing the username

$user++;


# if the incremented version exists, start again (note
# that the username is still incremented)


redo EXISTS if exists $hash{$user};


# if we've incremented the username to the point where
# it's now unique, then the redo will not run and
# we get to this point. Assign the hash value and
# last out of the loop.


$hash{$user} = $pw;
last EXISTS;
}
}
}
close DATA or die "Can't close data file $!\n";

for my $user (sort { $a <=> $b } keys %hash ){
print "User: $user Password: $hash{$user} \n";
}

# now you have a hash that's propagated with unique usernames
# as the key and the password as the key's value.




Tim Mousel
Deleted

Mar 23, 2001, 3:30 AM

Post #3 of 5 (453 views)
Re: Incrementing by one? [In reply to] Can't Post

Wow! Thanks for taking the time to write that! One more question....How do I get the $email and $date variables to print into my file?


#!/usr/bin/perl -w

print"Content-type:text/html\n\n";

my %hash;

open ( DATA, "</virtualhosts/www/join/data/memberlistconvert.txt" ) or die "Can't open data file $!\n";
while ( <DATA> ){
chomp;

my ( $user, $pw, $email, $daysleft, $date, $textpassword ) = split( /;/ );

unless ( exists $hash{$user} ){
$hash{$user} = $pw if $user;
}
else {
EXISTS : while ( each %hash ){
$user++;
redo EXISTS if exists $hash{$user};
$hash{$user} = $pw;
last EXISTS;
}
}
}
close DATA or die "Can't close data file $!\n";


open ( LOG, ">/virtualhosts/www/join/data/translog.txt" ) or die "Can't open data file $!\n";

open ( DATA, ">/virtualhosts/www/join/data/.htpasswd" ) or die "Can't open data file $!\n";

for my $user (sort { $a <=> $b } keys %hash ){


print DATA "$user:$hash{$user}\n";

print LOG "$date;$email;$user;$hash{$user}\n";


print "User: $user Password: $hash{$user} \n\n";

}

close(DATA);
close(LOG);



Jasmine
Administrator / Moderator

Mar 23, 2001, 9:41 AM

Post #4 of 5 (450 views)
Re: Incrementing by one? [In reply to] Can't Post


Code
#!/usr/bin/perl -w 

my %hash;

open ( DATA, "< users.txt" ) or die "Can't open users.txt $!\n";

while ( <DATA> ){
chomp;

# instead of assigning $user and $pw, we're going to assign only
# the username and place the remaining data in an array.


my ( $user, @data ) = split( /:/ );

unless ( exists $hash{$user} ){

# so now when the hash is propagated, we give it the value of
# the entire array (instead of the password only as in the
# previous example)


$hash{$user} = [ $user, @data ] if $user;
}
else {
EXISTS : while ( each %hash ){
$user++;
redo EXISTS if exists $hash{$user};

# Same here
$hash{$user} = [ $user, @data ];
last EXISTS;
}
}
}

close DATA or die "Can't close users.txt $!\n";

open ( NEW, "> usersnew.txt" ) or die "Can't open usersnew.txt $!\n";
for my $user (sort { $a <=> $b } keys %hash ){

# Now all we need to do is dererence the hash value (the value
# which is everything that was in @data for each user).
# you could have also done:
# my @array = @{$hash{$user}};
# and then you can treat the contents like a regular array.


print NEW join ( ";", @{$hash{$user}} ), "\n";
}
close NEW or die "Can't close usersnew.txt $!\n";

I'll leave it as an exercise to you to save different array elements to different files.



Tim Mousel
Deleted

Mar 23, 2001, 1:12 PM

Post #5 of 5 (445 views)
Re: Incrementing by one? [In reply to] Can't Post

Thank you, that worked perfectly. I truly appreciate the help!

Tim


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives