Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Always 'use strict;' and instead of <code>!# /usr/bin/perl -w</code>. Use <code>use warnings;</code>. It's a lot more flexible. I rewrote your program:</p> <p>Here's a few other hints:</p> <ul> <li>Don't use <code>$_</code> when you don't have to. There are problems with <code>$_</code> because of its <em>global</em> nature. It has to be used with <code>map</code> and <code>grep</code>, and people use it with <code>shift</code>, but most of the time, you end up having go assign it to something else anyway, so it doesn't do what it was suppose to do: Make your program <em>more readable</em>. In your case, you get an input, then assign it to <code>$_</code>. No need to do that. </li> </ul> <p>This works just as well:</p> <pre><code>$query_string =~ s/\W//gi; $query_string =~ s/\U$_/\L$_/gi; </code></pre> <ul> <li>You're getting <em>arrays</em> and <em>scalars</em> mixed up.</li> </ul> <p>This:</p> <pre><code>my @forward = reverse ($backwards[0]); </code></pre> <p>is assuaging your string <code>$backwards[0]</code> <code>$forward[0]</code>. There is no <code>$forward[1]</code>. The entire string is being assigned to the first element of the array.</p> <p>Maybe you want to do this:</p> <pre><code>my @forward = split //, reverse ($backwards[0]); </code></pre> <p>This splits the string <code>reverse ($backwards[0]</code> per letter, and puts each letter into <code>@forward</code>.</p> <p>I'm not sure what the input is. Why are you splitting on <code>=</code>? Does the user enter a palindrome?</p> <pre><code>whatisit=tisitahw </code></pre> <p>Or like this:</p> <pre><code>madam, I am Adam. </code></pre> <p>If it is the latter, why go through all of the conniptions?</p> <pre><code>#! /usr/bin/env perl use strict; use warnings; use feature qw(say); print "Enter String: "; chomp ( my $query_string= &lt;STDIN&gt; ); $query_string =~ s/[\W_]//g; if ( lc $query_string eq lc reverse $query_string ) { say "Hey, that's a palindrome"; } say "No, it's not a palindrome"; } </code></pre> <p>I use <code>s/[\W_]//g</code> to remove all spaces, and other non-letter stuff. It does keep numbers. The <code>[\W_]</code> is needed because <code>\W</code> doesn't include underscores. The <code>g</code> on the end means do this as many times as necessary in the string and not just the first occurrence.</p> <p>Then, all I have to do is compare the uppercase string to its reversed self. No need to step through an array. The <code>reverse</code> function handles everything for you. The only issue is that this is destructive because I munge the input string, so it doesn't match the user's input at the end of the program.</p> <pre><code>$ program.pl String: Madam I am Adam. No, it's not a palindrome $ $ program.pl String: Madam I'm Adam. Hey, that's a palindrome </code></pre> <p>What you should do is make this a function:</p> <pre><code>#! /usr/bin/env perl use strict; use warnings; use feature qw(say); print "Enter the string: "; ( my $string = chomp &lt;STDIN&gt; ); if ( palindrome( $string ) ) { say "Hey, $string is a palindrome"; } say "Sorry, $string is not a palindrome"; } sub palindrome { my $palindrome = shift; $palindrome =~ s/[\W_]//g; if ( lc $palindrome ne lc reverse $palindrome ) { return; # Not a palindrome } return $palindrome; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload