tanger has asked for the wisdom of the Perl Monks concerning the following question:

Hello, you might answered or read my other problem about this. But, i'm still having problems. In the newestm.txt it has a list of member names, and in the dbm file it has the members who are online as the keys (I changed it for the other people who answered me before). If the name appears in the newest.txt and also is a key in the hash then it'll print out the name like, Vegeta*.
#open newest open(NEW,"<$path/newestm.txt") or die "can't open $path/newestm.txt: $ +!"; @newest_members = <NEW>; close(NEW); #open whosonline use DB_File; tie %whop, "DB_File", "$path/whosonline" or die "Can't open FILENAME: +$!\n"; foreach $onname (@newest_members) { if (exists $whop{'$onname'}) { print <<EOF; $onname<b>*</b> - EOF } else { print <<EOF; $onname - EOF } } #foreach $newm untie %whop; dbmclose %whop;


This code does not work for somereason even though a member name is in the newest.txt and in the hash. Whats wrong?

Replies are listed 'Best First'.
(ichimunki) re: problem with exists
by ichimunki (Priest) on Feb 24, 2001 at 17:18 UTC
    Your code is not checking $onname it is checking '$onname'. No interpolation ever happens inside single 's. Change to $whop{$onname} or $whop{"$onname"}. The first one is idiomatic in this case, and the second should be reserved for cases where additional key information needs to added to the variable contents.

    Update: as chromatic points out, make sure to chomp your input from the file.

    As I said before, I also strongly recommend not using structures like
    print <<EOF; some text EOF
    unless absolutely necessary. If you use standard indentation for this code, you will mess it up. And if you are not indenting yet, get in the habit. When I see something like:
    if { ... } else { ... } }
    I have a hard time knowing what the second } matches.

    And just to make sure, you want this code to loop through a list of new members and tell you which ones are online. You are not expecting this code to loop through who is online and tell you which ones are on the new members list. right? I ask because unless I'm just plain not seeing something, it looks designed to do the former, and not the latter.
Re: problem with exists
by damian1301 (Curate) on Feb 24, 2001 at 09:44 UTC
    Sorry, but without an error message or more information on the problem I don't think there is anything to be done. I have a couple suggestions though:

    1. Indent your code - It makes it much easier and much clearer when dubugging
    2. on this line if(exists $whop{'$onname'}){...}, I would suggest using no quotes because single quotes do not allow variable interpolation. So it probably should be if(exists $whop{$onname}){...}.

    Other than that, I do not know. I hope I helped :)

    Almost a Perl hacker.
    Dave AKA damian

    I encourage you to email me
      Hello, I took the '' off.

      There were no error messages, just the fact that the code didn't work. I also just looked in the chatter box and saw this:

      crazyinsomniac> ok here goes my standard rand: tanger you bastard, be very very detailed when asking a question, and don't leave out the why/what/who/where /when/how and be sure to provide plenty of sample data

      Well I have teen online community where members could login and build there personal homepage, talk, and other member features. What I'm trying to do is when a new member signs up and logs in I want there name to be shown as, let say

      Vegeta33*

      The * indicates that theyre on. That member name would be in a table where includes new members. If you look at my first post it has the code on how to do this.

      Some how It does not work and I'm stuck once again.

      Anthony
        When reading lines from a file, it's usually necessary to chomp them before using them as hash keys, as they often have newlines.

        You could insert a debugging line in your foreach loop:

        print STDERR "Name is ->$onname<-\n";

        The extra punctuation will make any whitespace rather visible. chomp is probably exactly what you need, like chomp @newest_members; or chomp $onname;

Re: problem with exists
by unixwzrd (Beadle) on Feb 24, 2001 at 09:42 UTC
    Could it be you have a newline after each entry in "<$path/newestm.txt" and not in the hash key?

    Update:Didn't notice the ' in the hash lookup, but that would do it too...

    Another update: chromatic pointed out to me I left out the chomp, but I thought it was obvious, only after the 3rd or 4th time you miss it does it become obvious as chromatic mentioned...

    Mike - mps@discomsys.com

    "The two most common elements in the universe are hydrogen... and stupidity."
    Harlan Ellison