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

I'm very new to perl and I'm trying to write a game. When the user selects a square, the square should be "emptied". Here is the code:
#!/usr/bin/perl -w $a1 = 1; $a2 = 2; $a3 = 3; $b1 = 4; $b2 = 5; $b3 = 6; $c1 = 7; $c2 = 8; $c3 = 9; %squares = ("a1", $a1, "a2", $a2, "a3", $a3, "b1", $b1, "b2", $b2, "b3 +", $b3, "c1", $c1, "c2", $c2, "c3", $c3); print "Remove which one? "; chomp ($response = <STDIN>); make_blank(); print values %squares; print "\n"; print %squares; # makes the moved piece blank sub make_blank() { foreach $key (keys %squares) { ($squares{$key} .= "") if ($key eq $response); } }

edit by thelenm: changed title from "Newbie question"

Replies are listed 'Best First'.
Re: Newbie question
by jdtoronto (Prior) on Sep 17, 2003 at 18:53 UTC
    Try this:
    #!/usr/bin/perl -w use Data::Dumper; $a1 = 1; $a2 = 2; $a3 = 3; $b1 = 4; $b2 = 5; $b3 = 6; $c1 = 7; $c2 = 8; $c3 = 9; %squares = (a1 => $a1, a2 => $a2, a3 => $a3, b1 => $b1, b2 => $b2, b3 +=> $b3, c1 => $c1, c2 => $c2, c3 => $c3); print "Remove which one? "; chomp ($response = <STDIN>); &make_blank(); print Dumper %squares; print "\n"; print %squares; # makes the moved piece blank sub make_blank() { foreach $key (keys %squares) { ($squares{$key} = "") if ($key eq $response); } }
    But the basic problem was the . in the .= in make blank. The .= appends something to the scalar, in this case, nothing! It didn't make it blank.

    jdtoronto

    Updated purely to correct typo: s/.+/.=/

    Updated - also the sub make_blank is called before the prototype is seen, so you should add the & in front to stop the rror message appearing.

Re: Newbie question
by dsb (Chaplain) on Sep 17, 2003 at 18:59 UTC
    First of all, with regards to your hash %squares, I suggest you make it easier to read by making use of the => operator which acts just like a comma, but has the benefit of clarity in this case:
    %squares = (
        a1 => $a1, 
        a2 => $a2, 
        a3 => $a3, 
        b1 => $b1
        b2 => $b2
        b3 => $b3
        c1 => $c1
        c2 => $c2
        c3 => $c3
    );
    
    As to why your hash value is not being "emptied", in make_blank() what you are doing is concatenating nothing onto the value of the chosen square:
    $squares{$key} .= "";
    
    The .= is the concatenation operator. You want to use a regular assignment:
    $squares{$key} = "";
    
    Or just undef() it:
    undef($squares{$key});
    




    Amel
    This is my cool %SIG
Re: Newbie question
by tcf22 (Priest) on Sep 17, 2003 at 19:19 UTC
    Before you assign an empty string to the hash, you should check to see if it exists. If it doesn't, it will create that hash entry, and if you ever loop though the keys, you will get a user entered key. Which, I would assume you don't want.

    Also, you probably should use strict and just pass the user input into the make_blank() function.
    #!/usr/bin/perl -w use strict; my %squares = ( a1 => $a1, a2 => $a2, a3 => $a3, b1 => $b1, b2 => $b2, b3 => $b3, c1 => $c1, c2 => $c2, c3 => $c3 ); my $response = <STDIN>; chomp ($response); &make_blank(\%squares, $response); print values %squares; print "\n"; print %squares; # makes the moved piece blank sub make_blank() { my $sq_hashref = shift; my $res = shift; if(exists $sq_hashref->{$res}){ $sq_hashref->{$res} = ''; } }

    - Tom

Re: Newbie question
by Plankton (Vicar) on Sep 17, 2003 at 18:55 UTC
    maybe you should build your hash (%squares) like so ...
    %squares = ( a1 => $a1, a2 => $a2, a3 => $a3, b1 => $b1, b2 => $b2, b3 => $b3, c1 => $c1, c2 => $c2, c3 => $c3 );
    and since you are using a hash you can do this ...
    # makes the moved piece blank sub make_blank() { $squares{$response} = " "; }

    Plankton: 1% Evil, 99% Hot Gas.
Re: Newbie question
by blue_cowdawg (Monsignor) on Sep 17, 2003 at 18:48 UTC

    Ummm... very nice... but what is your question?


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
      Sorry. When I run it, it doesn't reassign the value to the variable in the hash. Any ideas?