Reversing a list/array and the CTRL+Z escape character --- David's Perl Project (Day 5 of 60)

I wrote the following after learning about lists & arrays.  Now, when I run it at a a DOS prompt, because it doesn't run well on the Perl Express IDE I'm using.  But when I run it in DOS, I have to hit CTRL+Z after typing my list, which gives me a ^Z symbol after my list, then I still have to hit ENTER one more time before the script continues, is there a way around this.  I just don't want to see the ^Z symbol and I don't want to have to hit ENTER again after hitting CTRL+Z, I thought it would simply execute the reversal and print out the results when I hit CTRL+Z.

#!/usr/bin/perl -w
print 'Please type a multi-line list, at the end of each line hit ENTER
and at the end of the list hit CTRL+Z followed by ENTER'."\n";
@list = <STDIN>;
@revList = reverse @list;
print "Your list backwards is:\n @revList";


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Program Files\Perl Express\Scripts>perl ch3-1
Please type a multi-line list, at the end of each line hit ENTER
and at the end of the list hit CTRL+Z followed by ENTER
Hello
World
This
is
David's
Program
^Z
Your list backwards is:
 Program
 David's
 is
 This
 World
 Hello

C:\Program Files\Perl Express\Scripts>
davidgareauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
I would use normal ASCII charcters to denote the end of usr input. Control charcaters are messy, Ctrl Z on unix menas susped job!

print 'Please type a multi-line list, at the end of each line hit ENTER
and at the end of the list type -1' ."\n";
my $finished = 0;
my @list;
while ( not $finished )
{
    my $val = <STDIN>;
    # detect end of entry with -1 or any other string that will not occur in your data, like EOI or _end
    if ( $val == -1)
    {
        $finished = 1;
    }
    else
    {
        push(@list,$val);
    }
}

my @revList = reverse @list;
print "Your list backwards is:\n @revList";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kim RyanIT ConsultantCommented:
You could also use pop to remove the last element in the array (the ^Z) , but still have to press enter after the Ctrl Z

@list = <STDIN>;
pop(@list)
@revList = reverse @list;
0
davidgareauAuthor Commented:
The ^Z doesn't show up in the reversed list that I did, so I don't need pop, and I think the exercise was meant to demonstrate using the <STDIN> in a list context. Do you have any ideas of how that could be done?  Because if I'm understanding all this correctly, then the line
my $val = <STDIN>
would be using the <STDIN> function (is it called a function?) in a scalar context.

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Kim RyanIT ConsultantCommented:
Correct, I am fetching input from the keyboard one entry at a time, and assigning to a scalar. The outer while loop keeps prompting fro more entries. By using STDIN, keyboard entry will only  cease after the user  hits enter.

If you want to do single charcater porcessing you could look at something like the Term::ReadKey module
0
wlfsCommented:
Actually, this is not an issue of perl but of the shell.
@list = <STDIN> reads in all of standard input from the shell. So perl has to rely on the shell to signal that the input from STDIN is finished. On Unix, this is easily done by pressing ^D (without another enter). On Windows, I don't know whether there is an equivalent without enter.

If you want perl to detect the end of the input by itself, you have to read it in linewise, as in teraplanes solution.
0
davidgareauAuthor Commented:
thanks guys, this is most likely just the fact that I'm using Windows for now and almost all Perl books are written by guys using Linux, so they probably give exercises without (understandably so) trying them all on Windows.

Regardless, the way you suggest makes sense and I will keep it in mind as a way to avoid this annoyance in the future, thanks.

david.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.