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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem using "eq" in "if" Statement within "foreach&qu 1

Status
Not open for further replies.

NigeW

Programmer
Jun 10, 2002
134
NZ
My Code
===========================================================
open (grades, "game_grades.txt") || die "Can't find database\n";
@data = <grades>;
close(grades);

open (games, &quot;game_games.txt&quot;) || die &quot;Can't find database\n&quot;;
@games = <games>;
close(games);

foreach $line (@data) {

print &quot;<TABLE BORDER='1' CELLPADDING='2' CELLSPACING='2'>&quot;;
print &quot;<TR>&quot;;
print &quot;<TD>Add Game for $line&quot;;
print &quot;</TD>&quot;;
print &quot;</TR>&quot;;

foreach $entry (@games) {

($grade, $date, $gtime, $teama, $teamacol, $teamb, $teambcol) = split(/\|/, $entry);

if ($grade eq $line) {
===========================================================
The text file game_grades.txt has 2 entries &quot;A Grade&quot; & &quot;B Grade&quot;
The text file game_games.txt has only one entry as follows
A Grade|January 07 2003|7:00pm|WipeOn|Red|WipeOff|Blue

So the foreach loop will run twice with $line holding the output each time

I then want to $line to $grade (output from game_games.txt) within the if statement

Using &quot;eq&quot; does not return any true matches (i.e. the game_games.txt entry should print when $list equals A Grade) and when I use &quot;==&quot; the game is returned for both variables under $line

Can anyone advise a better way to complete this comparison ?

Thanks

Nige
 
For debuging purposes try doing this

print &quot;<br>($grade eq $line)<br>&quot;;
haunter@battlestrata.com
 
Hi Haunter

Thanks for your reply.

I have tried the above and the output is nothing when using if ($grade eq $line) {.

When using if ($grade == $line) { the output is

(A Grade eq A Grade )
(A Grade eq B Grade)

Note there is an extra space inside the closing bracket on the first line of output - I have checked the text file containing the grades but there is no erroneous space in the file.

Now I'm confused

nige_w@clear.net.nz
 
You need to get rid of the newlines on the data that comes from the game_grades.txt file. You can do this either by chomping the whole @data array after you load the file into it:
[tt]
open (grades, &quot;game_grades.txt&quot;) || die &quot;Can't find database\n&quot;;
my @data = <grades>;
close(grades);
chomp @data;
[/tt]
Or you can chomp each line as you process it:
[tt]
foreach $line (@data) {
chomp $line;
[/tt]
jaa
 
That's got it.

Thanks justice41 for that

Upward and onward

This was my first post - do I need to close this now or does it get archived ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top