We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

davidgareau
davidgareau asked
on
Medium Priority
407 Views
Last Modified: 2008-01-09
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>
Comment
Watch Question

IT Consultant
Commented:
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";

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Kim RyanIT Consultant

Commented:
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;

Author

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.

Kim RyanIT Consultant

Commented:
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
Commented:
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.

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.