in reply to Printing two variables in the same line

First, use strict; and use warnings.

Next, there's no need to use @grades to initialize %grades. Just my %grades = qw( ... ) will work fine.

As for why it's not printing you've committed the classic mistake of using the assignment operator when you meant to test for equality. perldoc perlop and look for eq.

Update: And you also don't need to explicitly use the concatenation operator. Just "$_ has a $student\n".

Update: And really you don't need $student at all. Just exists $grades{ $_ } and print "$_ has a $grades{ $_ }\n" without it.

Replies are listed 'Best First'.
Re^2: Printing two variables in the same line
by radiantmatrix (Parson) on Oct 07, 2004 at 20:50 UTC
    Let me elaborate:
    if ($student = "") {print "Cannot find that student, try again\n";}
    Says, essentially, if the result of assigning the value "" to $student is true....

    The problem with that? $student = "" both returns a false value AND makes $student contain an empty string!

    The comparison operators are == for binary values (numbers, mostly) and eq for strings. What you really want is:

    if ($student eq "") {print "Cannot find that student, try again\n";} ## OR, Better unless ($student) {print "Cannot find that student, try again\n";}
    That way, $student will still hold the value you expect when you reach your print statement. If you use the latter version, it will also function properly if $student is undefined for some reason ("" and undefined are slightly different, but both false).

    It's a very common mistake, and hard to catch. The above advice to use strict; and use warnings; is sound. Also, learning to use the perl debugger might have helped you here.

    radiantmatrix
    require General::Disclaimer;
      Thank you for taking the time to elaborate and the examples you gave. It was a great help!!!
Re^2: Printing two variables in the same line
by JustLikeThat (Initiate) on Oct 07, 2004 at 22:04 UTC
    Thanks for the help. I'll learn more about the debugger soon.