in reply to Looking for pointers or optimizations.

1. Instead of this:

my $displayed; foreach my $s (split(//, $word)) { if (exists($correct_guesses{$s})) { $displayed .= $s; } else { $displayed .= "*"; } $displayed .= " "; }

This?

my $displayed = join" ", map { exists $correct_guesses{$_} ? $_ : '*' } split//, $word;

2. And instead of this:

print "Fails remaining: $turns\n"; print "Wrong guesses: ", join(" ", keys %wrong_guesses), "\n"; print "Your guess: ";

This?

print "Fails remaining: $turns\n", "Wrong guesses: ", join(" ", keys %wrong_guesses), "\n", "Your guess: ";

3. And instead of this:

if (!$guess =~ /[a-z]/ ...

This?

if ($guess !~ /[a-z]/ ...

Replies are listed 'Best First'.
Re^2: Looking for pointers or optimizations.
by thmsdrew (Scribe) on Aug 21, 2012 at 14:36 UTC

    In response to you first one, I have a similar optimization but a bit less confusing to me:

    my $displayed; foreach my $s (split(//, $word)) { $displayed .= exists($correct_guesses{$s}) ? "$s " : "* "; }