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

I got this question during perl test.

Write a program that accepts a number of ASCII values between 40 and 125 (both inclusive) and concatenates the corresponding characters to form a string. The program accepts a multi-line input. The first line contains a number representing how many ASCII values are in the input. The subsequent lines contain the ASCII values themselves (one ASCII value per line).

The following is an example of a valid input:

INPUT ------ 5 (this indicates the total number of ASCII values in the input set)

65

67

69

70

71

I have some other solution to this question. But i am very much confusing how to read multi line input in which the first line consider as number of times and subsequent lines contains ASCII values. I know my solutions is not correct, please help me in this regard.

#!/usr/bin/perl print "Enter the number of times : "; $times=<STDIN>; +@ascii=<STDIN>; foreach $s (@ascill) { print chr($s); }

I know the above solution is not suitable what question is asked above.

Thanks.

Replies are listed 'Best First'.
Re: Problem with reading multi-line
by BrowserUk (Patriarch) on May 01, 2010 at 22:53 UTC

    Although you've asked the user how many numbers to enter up front, and stored it in $times, you then make no use it at all.

    So when you do @ascii=<STDIN>;, perl doesn't know how many numbers to ask for, so It'll just keep asking for more until the user does something to stop it. For example, typing ^Z (or ^D on *nix?). But if the user enters more or less numbers before doing that, your program will get more or less than it is expecting.

    You need to modify your code so that it uses the value of $times to control how many times you ask the user for another number. Eg.

    #!/usr/bin/perl use strict; use warnings; print "Enter the number of times : "; my $times=<STDIN>; my @ascii; foreach (1 .. $times ) { push @ascii, scalar <STDIN>; } foreach my $s ( @ascii ) { print chr($s); }

    You'll get on much better if you format your code properly. And using strict and warnings will save you many hours of reshaping your plaster with your forehead.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      The OP said:

      concatenates the corresponding characters to form a string

      So this may be better:

      my $ascii; foreach ( 1 .. $times ) { $ascii .= chr <STDIN>; } print $ascii;
      Thanks to everyone here for replying.

      My solution was reading number of times in a variable called $times and then loop to read or print.

      If you read exam test solution as mentioned above, it read the input different way. For example @ascii is an array :

      $ascii[0]=3; (which mean read input 3 times) $ascii[1]=65; (read first input) $ascii[2]=66; (read second input) $ascii[3]=66; (read third input)

      Here automatically ends without pressing Ctrl+Z/D because as said first value ascii as number of times to read input.

      This is what I understand from the problem, please tell me if you have another solutions that can execute this task.

      Thanks to all. Iam a new comer to perl monks.
Re: Problem with reading multi-line
by Corion (Patriarch) on May 01, 2010 at 22:22 UTC

    Maybe you want to tell us what the problem with your solution is? Identifying the problem with what you have currently is the first step towards finding ways how to fix this.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Problem with reading multi-line
by ww (Archbishop) on May 02, 2010 at 01:54 UTC
    using the warn pragma will also spare you hunting for typos like that in line 2:
    @ascill
         ^^
    where your variable is actually @ascii
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Problem with reading multi-line
by Khen1950fx (Canon) on May 03, 2010 at 01:00 UTC
    I put this script together using the solutions given to you by BrowserUk, jwkrahn, and some ideas that I got from searching the monastery. Search can be a powerful tool...
    #!/usr/bin/perl use strict; use warnings; print "Enter the number of times : "; my $times= <STDIN>; my $prompt = q{Add values : }; my $maskPrompt = "\r" . ' ' x (length($prompt) + 2) . "\r"; my (@input) = (); print $prompt; chomp @input; print do { local $" = "\n -->"; " -->@input" }, "\n"; my $ascii; foreach $times(1 .. $times) { $ascii .= chr (<STDIN>); } print $ascii, "\n";
    Update: Condensed script. It should be:
    #!/usr/bin/perl use strict; use warnings; print "Enter the number of times : "; my $times= <STDIN>; my $prompt = "Add values : "; $prompt = "\r" . ' ' x (length($prompt) +2) . "\r"; print $prompt; my $ascii; foreach (1 .. $times) { $ascii .= chr (<STDIN>); } print $ascii, "\n";
      my $maskPrompt = "\r" . ' ' x (length($prompt) + 2) . "\r";

      Why do you create this variable if you never use it?

      my (@input) = (); print $prompt; chomp @input;

      Why do you assign nothing to an already empty variable?    And why then do you chomp an empty variable?

      foreach $times(1 .. $times) { $ascii .= chr (<STDIN>); }

      Why are you using the same variable name for the loop creation and for the local variable?    I have been using Perl for many many years and I have never seen anyone do this before and it is very confusing.    It only works because the value stored in $times is local to the loop body but it is not something that you should do because it is very confusing.

        I completely agree with you(jwkrahn) using the same variable name for the loop creation and even i never see such questions in my life. Believe me, all 10 questions which i was attempting to get Perl Certification from Expert Ratings has the same form of reading the input. I attempted test two times but failed.

        http://www.expertrating.com/certifications/PERL-Programming-Skills-Test.asp

        During this test, i tried to represent the code test several ways but nothing works.

        Thanks.