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 John Tel 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
 
Yep - if you're going to have hundreds, then leave it as an array...

You're welcome!
 
If it's going to be thousands of lines, there might be an argument for treating them one at a time rather than reading them all into memory first. This code
Code:
while(<>){
  next unless /[^a-d]/i; # neater regex
  print "Your input contains invalid sequence.\n";
  exit;
}
does exactly the same without the memory overhead. It also stops checking once it's found an invalid character whereas the previous version will always check all lines.

If you don't want to break the pipe from the input process, you could always
Code:
while(<>){
  next unless /[^a-d]/i;
  print "Your input contains invalid sequence.\n";
  @{<>}; # drain input
  exit;
}
although you could use the broken pipe to prematurely abort the errant input process and save even more cycles.

Yours,

fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top