Your Perl coding style is about ten years behind the curve. Things like lack of "use strict" and not using lexical file handles and three-argument open. If you want to seriously improve your Perl, I suggest you start by reading the excellent and free Modern Perl book.

I've tidied up your code and made a first attempt at converting it to a more modern style. The intent of your program is not entirely clear to me. Have a go at running the program below and let me know how close it is to solving your problem. See the comments below for more information on some of the changes I made to your original code.

# Always start your scripts with use strict and use warnings. use strict; use warnings; $| = 1; # autoflush your print statements # This subroutine slurps a text file into a string. # Note the use of a lexical file handle and three-argument open # and localization of the $/ global variable. # Note that the lexical $fh is automatically closed at end # of scope, i.e. when the subroutine exits. sub slurp_file { my $file = shift; open( my $fh, '<', $file ) or die "error: open '$file': $!"; local $/ = undef; return <$fh>; } # Note: you can use forward slashes on Windows (saves you escaping bac +kslashes). my $file = 'c:/Users/yashu/Desktop/SNLP/EX4/test.txt'; my $mytext = slurp_file($file); $mytext = lc $mytext; # $mytext =~ s/[^a-z|]/ /g; # your original replaces with space $mytext =~ s/[^a-z|]//g; # this deletes [^a-z|] instead my @wordarray = split //, $mytext; my $iter = 0; print "@wordarray\n"; # Note with a for loop you don't need the ugly array indexes. for my $wordchar (@wordarray) { while (1) { print " \n Enter the character then press ENTER : "; my $char = <STDIN>; chomp $char; if ( length($char) != 1 ) { print "you must enter exactly one character (not $char)\n"; next; } print "\n inputted character is : $char "; if ( $char eq $wordchar ) { print "Correct !!"; last; } print "\n Wrong please try again "; $iter++; print "\n Number of attempts : $iter "; } } print "\nNumber of failed attempts: $iter\n";
To input the character without having to press ENTER you would need to employ the Term::ReadKey module (see also perlfaq5 "How can I read a single character from a file? From the keyboard?").


In reply to Re: getc working twice . by eyepopslikeamosquito
in thread getc working twice . by yashavanth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.