Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with Foreach loop!!!!!!!!

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
0
0
GB
hi.
What i'm trying to do is keep a file of usernames, with numbers next to them:
-----------------
ben0
joe1
sophie2
etc...
----------------
when a user is added, the array is sorted using an inserytion sort, a number is assigned to the new entry, then the entire array is renumbered.
--------code-----------
chomp($username);
$username .= $pointer;
$username .= "\n";
push(@array, $username);
push(@array, @temp);
chomp(@array);
$inc = 0;

foreach $id (@array) {
$L = length($id);
$temp = substr($id, 0, $L - 1);
$id = "$temp$inc\n";
$inc++;
}
-------------------------
however, the file keeps getting corrupted ( as in, numbers on new lines, increasing amounts of numbers, etc...)
---------output ("C:/users.dat")--------
be0
1
joe2
john
3
sophie4
swanny5
zebedy6
----------------------------------------

so if anyone knows why this isn't working...a stray newline character, bad or non existent chomp etc. I'd be really grateful for all suggestions!!
thankyou all
JoZeF

we are all of us living in the gutter.
But some of us are looking at the stars.
 
JoZeF

I'm not really sure I understand where you input is coming from / going.

Is the entire script too large to post? If so, can you supply us a link, so we can get a feel for the whole time?

Thanks
 
well, here's the code :S
it is rather big and before i get posted an infinite amount of errors found etc, I already know about them :p
The bit I've already posted, is found towards the top of the script.
(in the 'username being sorted' bit).
Hope this helps :)
-------------------------------------------------
#!/usr/bin/perl

$anchor = 0;

main();
sub main {
$username = "";
$password = "";

print &quot;would you like to log in? (y/n) &quot;; chomp($answer = <stdin>); print &quot;\n&quot;;
if ($answer eq 'y') {
login();
}
elsif ($answer ne 'y') {
print &quot;would you like to create a new user? (y/n) &quot;; chomp ($answer2 = <stdin>); print &quot;\n&quot;;
}
if ($answer2 eq 'y') {
create();
}
elsif ($answer2 ne 'y') {
die;
}


sub create {
print &quot;what would you like your username to be? &quot;;

#<-----------------------Username is sorted------------------------>#

$username = <stdin>; print&quot;\n&quot;;
open(USERS, &quot;<C:/users.dat&quot;) || die &quot;couldn't open/write to user file!&quot;;
@array = <USERS>;
close(USERS);
$pointer = 0;
if ($username gt $array[$#array]) {
open(USERS, &quot;>>C:/users.dat&quot;) || die &quot;couldn't open/write to user file!&quot;;
chomp($username);
print USERS $username, $#array + 1, &quot;\n&quot;;
close(USERS);
$anchor = $#array + 2;
cont();
}

until ($username le $array[$pointer]) {
$pointer++;
}

open(USERS, &quot;<C:/users.dat&quot;) || die &quot;couldn't open/write to user file!&quot;;
@array = <USERS>;
close(USERS);
$anchor = $pointer;
$len = ($#array - ($pointer - 1));
@temp = (splice(@array, $pointer, $len));
chomp($username);
$username .= $pointer;
$username .= &quot;\n&quot;;
push(@array, $username);
push(@array, @temp);
chomp(@array);
$inc = 0;

foreach $id (@array) {
$L = length($id);
$temp = substr($id, 0, $L - 1);
$id = &quot;$temp$inc\n&quot;;
$inc++;
}

open (USERS, &quot;>C:/users.dat&quot;) || die;
print USERS @array;
close(USERS);
cont();
#<------------------end of sort-------------------->#

#<------------------Password sorted-------------->#
sub cont {
print &quot;your username is: $username\n&quot;;
print &quot;what would you like your password to be? &quot;; $password = <stdin>;print &quot;\n&quot;;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$leng = length($password);
open (TEMP, &quot;>C:/temp.dat&quot;) || die;
$z = 0;
until ($z == $leng) {
$temp1 = substr($password, $z, 1);
$tyme = ($sec + $min);
$temp2 = (ord($temp1)) * $tyme;
print TEMP $temp2, &quot; &quot;;
$z++;
}
print TEMP $tyme, &quot;\n&quot;;
close(TEMP);
open (TEMP, &quot;<C:/temp.dat&quot;) || die;
$line = <TEMP>;
close(TEMP);

open (PASSWORDS, &quot;<C:/passwords.dat&quot;) || die;
@Parray = <PASSWORDS>;
close(PASSWORDS);
$Plen = ($#Parray - ($anchor - 1));
@Ptemp = (splice(@Parray, $anchor, $Plen));
push(@Parray, $line);
push(@Parray, @Ptemp);
open (PASSWORDS, &quot;>C:/passwords.dat&quot;) || die;
print PASSWORDS @Parray;
close(PASSWORDS);

print &quot;your password is: $password\n&quot;;
main();
}

#<---------------------end of sort---------------->#

}
}

sub login {
open (USERS, &quot;<C:/users.dat&quot;) || die;
@users = <USERS>;
close(USERS);
open (PASSWORDS, &quot;<C:/passwords.dat&quot;) || die;
@passwords = <PASSWORDS>;
close(PASSWORDS);

print &quot;username: &quot;; $username = <stdin>; print &quot;\n&quot;; # <--------username requested

###############search test area####################

open(USERS, &quot;C:/users.dat&quot;) || die;
@users = <USERS>;
@index = $#users;
$i = 0;
until ($i == $#users) {
$index[$i] = $i;
$i++
}
close(USERS);
until ($#users == 0) {
$middle = int($#users / 2);
$mid = $users[$middle];
$top = $#users;
$bottom = 0;

if ($users[$#users] eq $username) {
found();
}
if ($username eq $mid) {
found();
}
elsif ($username gt $mid) {
splice(@users, $bottom, $middle);
}
elsif ($username lt $mid) {
splice(@users, $middle, $top);
}
if ($#users == 1&& $username ne $users[$#users]) {
sorry();
}
}
sub found {
chomp($username);
print &quot;WELCOME!!!!! $username!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n&quot;;
die;
}

sub sorry {
print &quot;sorry, you were not found :(\n&quot;;
main();
}
#######################################################################
} we are all of us living in the gutter.
But some of us are looking at the stars.
 
Here is a revised version of the sort part of the create() sub.
Code:
    #<-----------------------Username is sorted------------------------>#
    $username = <STDIN>;
    chomp $username;
    print&quot;\n&quot;;
    open(USERS, &quot;< users.dat&quot;) || die &quot;couldn't open/write to user file!&quot;;
    @array = <USERS>;
    close(USERS);
    @array = map { /(\w+?)\d+$/ } @array;
    $pointer = 0;
    @array = sort @array, $username;
    @array = map { $_.$pointer++.&quot;\n&quot; } @array;
    open (USERS, &quot;> users.dat&quot;) || die;
    print USERS @array;
    close(USERS);
    cont();
    #<------------------end of sort-------------------->#

jaa
 
To get the username to be presented to the user correctly you can add
Code:
($username) = grep { /$username/ } @array;
just before the
Code:
cont();

jaa
 
Another suggestion which would make things alot less complicated would be to use double delimitation with 2 characters, one delimitation with a colon to separate members, another with a comma to separate sub information. You could also even throw in their password if you wanted to.

joe,0,password:mary,1,password:tommy,3,password:etc,etc,etc:

This is what I use, you know that username is in array position 0, and their member number is in array position 1, and password is in position 2. You can call it up, dump it into an array, split it by /:/ to access a member, but keep in mind after the first split you have to split again with /,/ to access individual pieces of info within the string. Just a suggestion <i>Its okay to dream.....</i>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top