Re: Foreach loop
by Corion (Patriarch) on Sep 27, 2013 at 10:08 UTC
|
With the code and data you've shown, I cannot reproduce your problem:
#!perl -w
use strict;
my $key="5 10 14 16 17 20 25 26 38 1 42 4
+7 54";
my $substr = " 1 ";
my @newarray= $key;
foreach $key ( @newarray ) {
if (index($key, $substr) != -1) {
print "\nExist (substr)"
};
if ($key =~ /\Q$substr\E/) {
print "\nExist (RE)"
};
if ($key =~ /\s+1\s+/) {
print "\nExist (whitespace)"
};
}
__END__
>perl -w tmp.pl
Exist (substr)
Exist (RE)
Exist (whitespace)
Maybe your data in $key really has tabs in it, but you are searching for blanks? | [reply] [d/l] [select] |
Re: Foreach loop
by pvaldes (Chaplain) on Sep 27, 2013 at 11:03 UTC
|
use List::MoreUtils qw{any};
my @array = (1, 2, 3, 4, 5, 6, 7, "Silvester", 8);
print "I tawt I taw a puddy tat!\n"
if any {"Silvester"} @array;
| [reply] [d/l] |
Re: Foreach loop
by vinoth.ree (Monsignor) on Sep 27, 2013 at 10:02 UTC
|
if (my ($matched) = grep /^1$/, @array) {
print "found it: $matched\n";
}
Update:
Corrected the code as marked by Bloodnok
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
Re: Foreach loop
by Marshall (Canon) on Sep 27, 2013 at 11:37 UTC
|
Update: Op tells me that he wants index of the "1"
Here is one way to do that...added to previous code that only reports existence of a "one"
#!/usr/bin/perl -w
use strict;
use List::Util (qw/first/); #a core function in all Perl's
use List::MoreUtils (qw/first_index/); #not core but a common module
#first{} stops at the first occurrence, slightly faster than grep{}
#first_index() reports index of {block}
while (my $line = <DATA>)
{
my @array = split /\s+/, $line;
print "A One is in: $line" if (first {$_ == 1}@array);
my $index = first_index{$_ == 1}@array;
print "index of the One=\[$index\]\n" if $index >-1;
}
=output
A One is in: 5 10 14 16 17 20 25 26 38 1 42 47 54
index of the One=[9]
A One is in: 5 1
index of the One=[1]
A One is in: 1 2342
index of the One=[0]
=cut
__DATA__
5 10 14 16 17 20 25 26 38 1 42 47 54
5 10 14 16 17 20 25 26 38 42 47 54
5 1
4 10
1 2342
34 55
| [reply] [d/l] |
Re: Foreach loop
by Bloodnok (Vicar) on Sep 27, 2013 at 10:42 UTC
|
If you are going to use a for loop, I'd use something along the lines of ...
my $keys = "5 10 14 16 17 20 25 26 38 1 42
+ 47 54";
my $substr = '1';
print "\nExist" if $_ eq $substr for split /\s+/, $keys;
Having said that, the OP has the whiff of homework
A user level that continues to overstate my experience :-))
| [reply] [d/l] |
|
|
| [reply] |
Re: Foreach loop
by marinersk (Priest) on Sep 27, 2013 at 14:03 UTC
|
Your need to work around whitespace is a Perl speciality. This looks like a C programmer's way to attempt the search; free your mind and embrace the Perl way:
Instead of index, use grep. The \s characters will accept any kind of whitespace, be it space character or tab, etc.
This is the change I'd recommend:
OLD: $substr = " 1 ";
NEW: $substr = '1';
my $regex = quotemeta $substr;
OLD: if (index($key, $substr) != -1) {print "\nExist";}
NEW: if (grep /\s$regex\s/, " $key ") {print "\nExist";}
Update: In order to ensure a leading or trailing '1' is captured, the grep was modified to include forced whitespace around $key.
| [reply] [d/l] |