Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<STDIN> for array 4

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
#! /usr/bin/perl
use strict;
use warnings;

my @SeqChar;
chomp(@SeqChar = <STDIN>);

sub is_invalid_list {
return scalar grep /[^a-dA-D]/, @SeqChar;
}

print "Yes\n" if is_invalid_list( qw/yes daad cab/ );

Why is it not returning anything?

jl
 
oh!

The input is hundreds and thousands of qw /abcd/ or qw /ABCD/.

jl
 
I'm a little puzzled by your line:
Code:
print "Yes\n" if is_invalid_list( qw/yes daad cab/ );
You are passing arguments to the "is_invalid_list" function, but the function is not using those arguments - just what is found in your SeqChar array.

Also - it returns output for me. What OS are you running this on?
The purpose of this code is to display Yes if the strings you pass it do not start with a-d or A-D.
If you are passing it strings that start with a,b,c,d,A,B,C,D then you will not see any output.
 
I am using FreeBSD.

#! /usr/bin/perl
use strict;
use warnings;

my @SeqChar;
chomp(@SeqChar = <STDIN>);

sub is_invalid_list {
return scalar grep /[^a-dA-D]/, @SeqChar;
}

print "Yes\n" if is_invalid_list();

This is still not returning output.

jl

 
Okay - try this at the command line:
Code:
echo "ABCD" | yourprogram.pl
and you will get nothing - which is correct.
Now try this:
Code:
echo "ZYXW" | yourprogram.pl
and it should return Yes!
because this string does not start with an a-d
 
Oh, and I have a correction to my statements. I've been saying as long as the strings do not start with a-d or A-D. It should be if the strings do not contain a-d or A-D.
 
@Brian - that's not what the regexp is testing for. It will print "Yes" if the string contains any character other than a,b,c,d (upper or lowercase) anywhere in it. In that regexp, the caret is to create a negative character class, not to anchor to the start of the string.
 
arrrrgggg,

What is missing here?

-bash-2.05b$ echo "ZYXW" | testchar.pl
-bash: testchar.pl: command not found
-bash-2.05b$ echo "ABCD" | testchar.pl
-bash: testchar.pl: command not found

jl
 
Sorry - if the current directory is not in your path, then put a ./ in front of the program like this:
Code:
echo "ZYXW" | ./testchar.pl
echo "ABCD" | ./testchar.pl

Make sure the program is executable too
 
my @SeqChar;
chomp(@SeqChar = <STDIN>);

sub is_invalid_list {
return scalar grep /[^a-dA-D]/, @SeqChar;
}

print "Your input contains invalid sequence.\n" if is_invalid_list();

It returns output with ‘echo’, but when I type in the input on the command line, why does it keep receiving input and not returning anything????

-bash-2.05b$ ./testchar.pl
wewerwerwe
werewrwerwe
ertyer
1
exit

^C


jl

 
If you are typing a control-C to end your stream, that may be the problem. That usually breaks the program. Instead, use control-D to signify the end of your series.
 
Pressing enter only adds a newline character to your input. Ctrl-D signifies that you're finished entering input. Ctrl-C cancels the whole thing.

@Brian - bah! beaten by less than a minute :)
 
If you were to look at what was in the STDIN stream, everywhere you hit the Enter key, you would see a \n, which is the newline symbol. Your program is built to accept the newline character, and then "chomp" them off (using the chomp command).
control-D usually signifies the end of stream.
 
Then, is this (Enter sequences and 'enter' and then Ctrl + D) only way?? I like to make 'enter' execute the pl.

jl
 
Since you are pulling your <STDIN> stream into an array, the program is looking for multiple lines of input, until it sees the control-D.

If you are just looking for one line of input, then change the @ to a $ (@SeqChar to $SeqChar) throughout your program - that will end on an enter.
 
since the input can be hundreds and thousands of strings, it should be @SeqChar then? Am I correct on that?

jl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top