Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Foreach loop

by PetreAdi (Acolyte)
on Sep 27, 2013 at 09:55 UTC ( #1055951=perlquestion: print w/ replies, xml ) Need Help??
PetreAdi has asked for the wisdom of the Perl Monks concerning the following question:

How can I check if the integer value 1 is in the array

$substr = " 1 "; foreach $key ( @newarray ){ if (index($key, $substr) != -1) {print "\nExist";} }

What is wrong ?

@newarray[n]="5 10 14 16 17 20 25 26 38 1 42 + 47 54";

Don't find because substring contains whitespace

Comment on Foreach loop
Select or Download Code
Re: Foreach loop
by vinoth.ree (Parson) on Sep 27, 2013 at 10:02 UTC

    Use grep with ^ and $ to match exactly 1

    if (my ($matched) = grep /^1$/, @array) { print "found it: $matched\n"; }
    Update:

    Corrected the code as marked by Bloodnok


    All is well
      Hmmm,

      I think you meant to say grep /^1$/, @array

      A user level that continues to overstate my experience :-))

        Oops! Yes Bloodnok, I have not tested it. Thanks.


        All is well
      OP doesn't have an array.

      Did you try this with search values other than '1'?

Re: Foreach loop
by Corion (Pope) 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?

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 :-))
      It might have the whiff of homework, but at least the OP posted functional code. It leaves us able to prod in the right direction without necessarily giving the whole house away.

      But yeah, some care was warranted. :-)

Re: Foreach loop
by pvaldes (Chaplain) on Sep 27, 2013 at 11:03 UTC

    How can I check if a value "X" is in the array?

    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;
Re: Foreach loop
by Marshall (Prior) 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
Re: Foreach loop
by marinersk (Chaplain) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055951]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-10-02 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls