--- "Curtis, Jonathan" <[EMAIL PROTECTED]> wrote: > This subroutine takes the user's login name ($ntlogin) and searches the flat > file for matching entry. > > The flatfile contains user info, comma delimited... each user separated by > line. The flatfile is read by line into @data, then splits each field of > @data by comma into @cheese as it searches for the correct login name > > so values in cheese would look like > $cheese[0] = jcurtis > $cheese[1] = Jonathan Curtis > $cheese[2] = <mailto:[EMAIL PROTECTED]> [EMAIL PROTECTED] > .... > > The loop should return a flag ($found =1) and the line number ($eindex) of > th sub have_we_met{ my $ntlogin = shift; die "\$ntlogin is undefined!" if ! defined $ntlogin; my ( $found, $eindex ) = 0; my ( $name, $email ); open DATA, "< bigdawgs.txt" or die "Cannot open bigdawgs.txt for reading: $!"; while (<DATA>) { my @cheese = split /,/; if ($cheese[0] eq $ntlogin) { $found=1; $eindex=$i; ( $name, $email ) = @cheese[1,2]; last; } $eindex++; } close DATA; $eindex = undef if ! $found; return ( $found, $eindex, $name, $email ); }e user's data... > -- > > sub have_we_met{ > open DATA, "bigdawgs.txt"; > @data = <DATA>; > close DATA; > $ > $found = 0; > > for ($i = 0; $i <= $#data; $i++){ > @cheese = split /,/, $data[$i]; > if ($cheese[0] eq $ntlogin) { > $found=1; > $eindex=$i; > } > } > > } > > ---
It's terribly important to ensure that you are indenting well and match curly braces with the proper scope. Otherwise, it's difficult to be sure which curly brace matches which scope. In this case, however, this does not appear to be your problem. > The line in bold "if ($cheese[0] eq $ntlogin) {" causes error "Use of > uninitialized value..." for each repetition of the loop. > I know $ntlogin is initialized cause it's used all over the place... > I know @cheese is getting info, because I can print it's contents. Jon, >From what you are saying, it appears that you are using $ntlogin as a global >variable. This is generally bad practice because it makes it more difficult to find out what part of your code might be changing the value of $ntdata. When writing subroutines, it's a good practice to make them little black boxes. Everything you need gets passed to it and it returns everything you expect, without resorting to globals. This gives you fine-grained control of the functions and makes it easier to reuse them. What follows is a quick rewrite of your code. First, the subroutine: 01 sub have_we_met{ 02 my $ntlogin = shift; 03 die "\$ntlogin is undefined!" if ! defined $ntlogin; 04 my ( $found, $eindex ) = 0; 05 my ( $name, $email ); 06 open DATA, "< bigdawgs.txt" or die "Cannot open bigdawgs.txt for reading: $!"; 07 08 while (<DATA>) 09 { 10 my @cheese = split /,/; 11 if ($cheese[0] eq $ntlogin) 12 { 13 $found=1; 14 ( $name, $email ) = @cheese[1,2]; 15 last; 16 } 17 $eindex++; 18 } 19 close DATA; 20 $eindex = undef if ! $found; 21 return ( $found, $eindex, $name, $email ); 22 } In line 02, we explicitly pass in $ntlogin. This has a local scope and will not affect any other value of $ntlogin anywhere in the script. Line 03 dies if this value is undefined. Very useful for error checking. Lines 04 and 05 set up some variables that we're going to use in this sub. These variables will later be returned. $eindex is probably superfluous as I suspect you were using this to find the appropriate index in @data in order to get the name and email. If this is what you were doing, you don't have to return this. Line 06 opens the file for reading and dies if the file is not found. For more general use, you should either pass in the file name or declare it as a constant at the top of your code. Lines 08 through 18 iterate over over the incoming data, looking for the $ntlogin. I've changed the name of the filehandle to USER_DATA to avoid potential problems with the __DATA__ token. Note that if you have a large data file, the @data array could be quite huge. The new subroutine doesn't bother to read the entire file at once, thus saving memory. Lines 11 through 16 check to see if $ntlogin matches. If so, we set $found to a true value and use an array slice in line 14 to set the $name and $email. Then we *last* out of the while loop. Since we've found what we need, there's no reason to continue. Line 17 increments $eindex, but I suspect that you won't need this, unless the line number is important. Side note: $eindex can be replaced with the special $. (dollar dot) variable. This variable is the current input line number of the last filehandle read. However, I didn't use it as it seems to make many people hyperventilate :) Line 19: close the file. Line 20: can probably be discarded. Line 21: return the data. Note that all values other than $found will be undef if nothing is found. Heck, we could probably skip $found and $eindex and in your subsequent code, check to see if the subroutine returned defined values for $name and $email. If not, you know the name wasn't found. I think that's better, but I wanted to keep this reasonably close to what you had. Here's how you call the sub: 01 my ( $found, $eindex, $name, $email ) = have_we_met( $ntlogin ); 02 03 if ( ! $found ) 04 { 05 # error handling 06 # all values except $found will be undef 07 } 08 else 09 { 10 # it was found, proceed as normal 11 } That should be pretty straightforward. Incidentally, here's something a little closer to how I would write this: sub have_we_met { my ( $ntlogin, $user_data ) = @_; die "\$ntlogin is undefined!" if ! defined $ntlogin; my ( $name, $email ); open USER_DATA, "< $user_data" or die "Cannot open $user_data for reading: $!"; while (<USER_DATA>) { my @cheese = split /,/; if ($cheese[0] eq $ntlogin) { ( $name, $email ) = @cheese[1,2]; last; } } close DATA; return ( $name, $email ); } my ( $name, $email ) = have_we_met( $ntlogin, $filename ); if ( ! defined $name ) { # error handling # all values except $found will be undef } else { # it was found, proceed as normal } If you have any questions, let us know. Cheers, Curtis "Ovid" Poe ===== "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __________________________________________________ Do You Yahoo!? Great stuff seeking new owners in Yahoo! Auctions! http://auctions.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]