Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] Scripting Help

Expand Messages
  • erik.tank@bpxinternet.com
    You are right ... I forgot that when you read in from the standard in to an array it goes until it receives an end of line character. (FYI: if you read in from
    Message 1 of 4 , Jun 28, 2001
      You are right ... I forgot that when you read in from the standard in to an
      array it goes until it receives an end of line character. (FYI: if you read
      in from standard in to a scalar - $text = <STDIN>; - then Perl will only
      allow you to enter in 1 line.) Ctrl-D is the end of line character on UNIX
      and Ctrl-Z is the end of line character on DOS. This way you can either
      enter the variables on the command line as you are doing or you can simply
      put the input into a file and redirect it into the script (that is what my
      command is doing). I bet this is how you instructor will test you programs.
      Unless of course he is a masochist ;-).

      Also it is redundant to execute your script with "perl script_name" if the
      first line of you program is "#!/usr/location_of_perl_interpreter/perl".
      But of course if you use this method of execution then you don't need to
      know the actual path of the Perl interpreter nor do you need to worry about
      file permissions to much.

      As far as getting inconsistent outputs I am not sure why. Try putting his
      example input into a file and then running it with the redirect. If it
      still doesn't work then you might have some of the input values entered
      incorrectly. If you have done this and it still is not working for you feel
      free to email me your input file and you Perl script and I will take a look
      at it.

      Erik Tank

      -----Original Message-----
      From: cyberdiction@... [mailto:cyberdiction@...]
      Sent: Wednesday, June 27, 2001 3:57 PM
      To: perl-beginner@yahoogroups.com
      Subject: Re: [PBML] Scripting Help

      --- In perl-beginner@y..., erik.tank@b... wrote:
      > Let me add the comments to what is happening. Is there a specific
      > you don't understand? I do have to admit that I had to reread the
      > and execute the program before I finally understood what the
      instructor is
      > asking for.
      > The test input that you listed below is contained in a file and
      this program
      > is executed this way: ./code_file < test_data_file


      Thanks for your speedy reply! I am still contemplating your
      reply. I got stuck right away at the top of your response.
      I made progress in understanding this from the rest of your answer.

      Are you saying that there is a file in the same directory
      that perl is using as input? That is how I thought the problem
      would be solved but the instructor said no. I think that the
      input is generated within the perl script itself, from the keyboard:
      You are right about the $i=0; typo. This is a screen capture.

      C:\xemacs>perl pearly.pl #then I input these lines at the blank lines

      #then ctrl-z outputs these lines or ctrl-d in emacs



      #But for some reason it outputs just Red(no Blue) sporadically.

      > 1: #!/usr/bin/perl
      > 2: chomp (($s, @a)=<STDIN>);

      > 3: for ($i=0; $i < $s; $i++) { #Note that I replace '$i=0,'
      with '$i=0;'
      > 4: print "$a[$a[$i]+$s]\n";
      > 5: }
      > Line 2: Several things are happening here. It works just like
      math, we do
      > what's inside the parentheses 1st. ($s,@a)= <STDIN> takes input
      from the
      > standard in (usually the key board, but since we have executed the
      > the way we did it is taking the input from our test_data_file).
      Perl (as
      > default setting) using the newline character as a signal that
      something has
      > been entered and it going to do something with it. So it takes the
      > line and puts it into $s, since the next variable in the list is an
      > the rest of the lines are dumped into it with each line becoming an
      > in the array.
      > Next thing that happens is that the list ($s, @a) gets chomped this
      > that the newline character is removed from each entry in the list
      if the
      > entry ends with a newline character. Note that every element of @a
      > chomped individually also.
      > Line 3: sets up a loop. In our case $i will have the values: 0,1,2
      > Line 4: prints the array element that is referenced. You are right
      > the undefined, but that just means that nothing gets printed out
      with a \n
      > (newline character) after it. This is where the empty line comes
      > Hope that helps a little.
      > Erik Tank
      > 602-817-4705
      > erik.tank@b...

      Thank you!

      > -----Original Message-----
      > From: cyberdiction@y... [mailto:cyberdiction@y...]
      > Sent: Wednesday, June 27, 2001 12:05 PM
      > To: perl-beginner@y...
      > Subject: [PBML] Scripting Help
      > Hello,
      > I only had a dab of programming in my Intro to Unix class and
      > am finding myself severely challenged in my Intro Perl class.
      > I was not able to understand or do the following homework
      > assignment. I will provide the problem plus the instructor's
      > script code. What I would like is for a few people to write
      > longer ways of doing this that use different loops and methods.
      > I want to see a few different solutions to do the same thing.
      > Problem: This program will read in a list of numbers and strings
      > (lines of text) from standard input and then output the lines of
      > text as specified by the numbers. Input to the program is an
      > integer that specifies the number of integers that follow. Next
      > is the list of numbers. This is followed by the lines of text.
      > Each input item is on its own line. The first line of input text
      > counts as line number 0. Use the list of numbers to create your
      > output--output a blank line where the integer is not a valid line
      > number.
      > Sample input:
      > 3
      > 1
      > 40
      > 0
      > Red
      > Blue
      > Green
      > Yellow
      > Purple
      > Brown
      > Output should be:
      > Blue
      > Red
      > Instructors solution:
      > #!/usr/bin/perl
      > chomp (($s, @a)=<STDIN>);
      > for ($i=0, $i < $s; $i++) {
      > print "$a[$a[$i]+$s]\n";
      > }
      > Would a few people post alternate ways of scripting
      > this result, preferably with comments. The instructor
      > explained the script but did not comment it and I was
      > not able to reconstruct how to comment it; it was quick.
      > I think the blank line between Blue Red is because of undef.
      > Thank you for any basic insights. "Learning Perl" and
      > the Perl FAQ (which helped) have been consulted already.
      > Yet another less than elegant perl scripter,
      > Stephen
      > Your use of Yahoo! Groups is subject to

      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    Your message has been successfully submitted and would be delivered to recipients shortly.