LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2013, 04:55 PM   #1
gregAstley
LQ Newbie
 
Registered: Aug 2012
Distribution: ubuntu 11.10
Posts: 27

Rep: Reputation: 4
perl boolean seems to evaluate as undef


I am following the exercises in the O'Reilly Learning Perl book and one of them asks to write a sub greet, taking one parameter (a name) that supplies a greeting if this is the first $name that was passed to the sub, otherwise supply a greeting along with the other names that have been sent previously. (I also check if the name has been sent more than once). I am having problems getting into a certain bit of code however - I have commented where I am seeing the problem
Code:
#!/usr/bin/perl -w

use v5.14.0;

sub greet {
    my $iName = $_[0];
    #need some state variable that stores the parameters that have already been sent to this function on a previous invocation
    state @seen_names;
    #This would all be better implemented with a hash as the container type 
    #but in the spirit of working with beginning example sets lets just use the simpler data structures.

    if (!@seen_names) {
        push @seen_names, "$iName ";
        print "Hello $iName, you are the first visitor to this function.\n";
    } else {
        my $push_name = 0;
        foreach my $el (@seen_names) {

#------------------------DEBUG-----------
            print "equal? ", ("$el" eq "$iName"), "\n"; #debug - the boolean looks undef
            print "equal? ", ("a" eq "a"), "\n"; #debug - the boolean is true
#------------------------DEBUG-----------

            if ($el eq $iName) { #debug - do not get into this if
               # FIXME the next line will reprint this name unless I loop through it again from scratch (or delete then re-add after)
               # neither is nice
               print "hello again $iName, you have been here before, other visitors have been " , @seen_names, "\n";
            } else {
                print "hello $iName, previous visitors were @seen_names.\n";
                $push_name = 1;
            }
        }
        if ($push_name) {
            push @seen_names, "$iName ";
        }
    }
}

&greet ("Greg");
&greet ("Greg");
#&greet ("Bob");
#&greet ("James");
#print &greet "Jill";
~
what am I doing wrong?


EDIT: It seemed like a good idea at the time to add each name with a space (so I could print the array without writing another loop to avoid all the names being stuck together) but that means the if I was having trouble with evaluates as false (though false doesn't seem to be 0 as I expected).

Last edited by gregAstley; 12-01-2013 at 06:51 AM. Reason: getting rid of infinite loop
 
Old 12-03-2013, 04:54 AM   #2
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
I think your problem is that, on the first greet, you push "Greg " rather than "Greg" onto @seen_names. Second time around, $el and $iName are not eq because one is "Greg " and the other is "Greg".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cross-Compiling on ARM from x86_64: Undef ref to rplmalloc/realloc johnp Linux - Software 3 07-13-2013 05:04 PM
Undef handle weirdness in Net::SSH::Perl PenguinPwrdBox Programming 0 07-11-2006 01:11 PM
boolean question! xemous General 3 03-18-2005 03:40 AM
Unknown boolean error mebrelith Fedora 4 01-24-2005 03:47 AM
help with #ifdef #undef etc tuxfood Programming 3 06-29-2004 08:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration