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

I cannot get the kinks worked out of the following program. I have been reading three different reference books plus loads of online tutorials and I learn best by doing. So I decided to use my knew knowledge of associative arrays to write a program that would

1.)Prompt the user to type in their name

2.)Take the name from input and compare it to an associative array of names+values

3,)Respond to the name from input according to it's relationship to the data in the array

4.)Print out the key+values ina sentence at the end telling the user their name followed by their role in my family.

#!/usr/local/bin/perl #Family.pl #use strict #use warnings $name=<stdin>; %family['Connie',=>'Mama','Brandy',=>'Sissy','Pete',=>'Dude','Tristan' +,=>'Stinky', 'Sophera',=>'Finky','priscilla',=>'BearDawg']; print"Please type in your name:"/n; $name=<stdin>/n; if((name=<stdin>)!=%family[]) { print"Thank you $name for participating but you are not family./n"; } else-if(($name=<stdin>)==%family[]) { print"Hey $name, thank you for being my %family[]!\n";

Replies are listed 'Best First'.
Re: Using associative arrays with I/O to interact with Prog. User
by toolic (Bishop) on Dec 01, 2008 at 16:52 UTC
    Here is a cleaned-up, working example of your code to get you started:
    use strict; use warnings; my %family = ( 'Connie' => 'Mama', 'Brandy' => 'Sissy', 'Pete' => 'Dude', 'Tristan' => 'Stinky', 'Sophera' => 'Finky', 'priscilla' => 'BearDawg' ); print "Please type in your name:\n"; my $name = <STDIN>; chomp $name; if (exists $family{$name}) { print "Hey $name, thank you for being my family!\n"; } else { print "Thank you $name for participating but you are not family.\n +"; }
Re: Using associative arrays with I/O to interact with Prog. User
by moritz (Cardinal) on Dec 01, 2008 at 16:27 UTC
    Please use <code>...</code> tags around your code.
    #use strict #use warnings

    That's the first mistake: don't comment out these lines, they are your first sanity check. And declare your variables with my.

    $name=<stdin>;

    Did you mean STDIN instead? Perl is case sensitive.

    %family[...

    That's not Perl syntax. Please read perlintro, perlsyn and perldata. Or even buy yourself a good Perl book, like "Learning Perl".

      Perl still supports the lower case forms of stdin, stdout and stderr:

      $ perl -le'/std/i && print for keys %::' stderr stdout stdin STDOUT STDERR STDIN $ perl -le'print stdout "Hello" or die $!' Hello

      But to get it to work with strict and warnings enabled is a bit more difficult:

      $ perl -Mwarnings -Mstrict -le'print {*stdout} "Hello" or die $!' Hello $ perl -Mwarnings -Mstrict -le'print {\*stdout} "Hello" or die $!' Hello
Re: Using associative arrays with I/O to interact with Prog. User
by cdarke (Prior) on Dec 01, 2008 at 16:54 UTC
    You have numerous syntax errors and mistypings, but you are getting there so persist! I know it is a pain to begin with, but please do not comment out use warnings and use strict. Try to understand why warnings and error messages are produced - they are your friends.

    Here is my version to be getting on with, but always remember TMTOWTDI:
    #!/usr/local/bin/perl #Family.pl use strict; use warnings; my %family = ('Connie' =>'Mama', 'Brandy' =>'Sissy', 'Pete' =>'Dude', 'Tristan' =>'Stinky', 'Sophera' =>'Finky', 'Priscilla'=>'BearDawg'); print"Please type in your name: "; my $name = <STDIN>; chomp $name; # It's good to chomp # Convert to lower case with uc first char $name = ucfirst(lc $name); # Check for the absence of $name as a key if( !exists $family{$name}) { print"Thank you $name for participating but you are not family.\n" +; } else { print"Hey $family{$name}, thank you for being family!\n"; }
    update: corrected chop to chomp (thank-you toolic)
Re: Using associative arrays with I/O to interact with Prog. User
by eighty-one (Curate) on Dec 01, 2008 at 17:11 UTC

    I created a cleaned up version of your code, but toolic beat me to it. His version is identical to what I cam up with, except mine lacked the  exists in the if, so I won't bother putting mine here

    I found this article, The Perl Hash How-To, to be very valuble. I used to keep a copy pinned to the wall next to my desk because I was always getting screwed up by syntax.

    As for your program, you had some misplaced commas and brackets instead of parenthesis where you created your hash. You had forward slashed where you needed backslashes for the newlines, and you had newlines in odd places (the newline escape sequence, '\n', needs to be in doublequotes to be interpreted as you expect).

    Also, you need to chomp your input, otherwise you end up with a trailing newline character that will cause a mis-match, even if you enter what you think should be a match.

    I'm not too familiar with Ruby - is the style of comparison you use how you determine if a value is in a hash in that language? I've not looked at too much Ruby code but I seem to recall seeing something similar. Here a node about comparing hash values, and an article at Perl.com with some good has info.

    It looks like Perl syntax is your stumbling block, as well as perhaps habits learned from another language. It shouldn't take much more than a bit of practice, and maybe reading over someone else's code or some Perl documentation to get past that :)