Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-02
6
Medium Priority
?
367 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>
0
Comment
Question by:davidgareau
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 1200 total points
ID: 16357074
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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16357119
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
 

Author Comment

by:davidgareau
ID: 16357259
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16357508
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
 
LVL 5

Assisted Solution

by:wlfs
wlfs earned 800 total points
ID: 16365510
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
 

Author Comment

by:davidgareau
ID: 16388900
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question