# 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.

# 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.