?
Solved

finding element in a array

Posted on 2005-04-29
9
Medium Priority
?
8,168 Views
Last Modified: 2011-08-18
i have aray
@test = ("java", "visual", "pascal", "cobol");

how do i find if the the element visual exists in array or not.

i know to do thru for loop, but do we have any function for arrays

Rahul
0
Comment
Question by:shahrahulb
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Author Comment

by:shahrahulb
ID: 13897052
got it:

grep(/rahul/, @arr)
0
 
LVL 1

Author Comment

by:shahrahulb
ID: 13897057
Please close this question

Thanks.
0
 
LVL 13

Assisted Solution

by:gripe
gripe earned 600 total points
ID: 13897069
There is no way to do this without looping. You can use grep, but it does the same thing behind the scenes. This is also a faq (perldoc -q contains):

for (@test) { print "Match" if $_ eq "visual"; last;} # Find the match, and exit the loop
($is_there) = grep $_ eq "visual",  @test; # Bad practice, every element is checked regardless of whether the match is found.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:gripe
ID: 13897081
shahrahulb,

grep is worse than looping. In a grep() every element of the array is checked whether it matches or not. if you do a for () {} loop, you can exit the loop before checking the remaining elements. (As above)
0
 
LVL 13

Expert Comment

by:gripe
ID: 13897089
It's also even worse practice to do a regex match, since a regex match will be much slower than a standard string compare with 'eq'.

All of this is covered in the faq.
0
 
LVL 13

Assisted Solution

by:gripe
gripe earned 600 total points
ID: 13897101
Sorry, my example should read:

for (@test) {
    if ( $_ eq "visual" ) { print "Match"; last; }
}
0
 
LVL 85

Accepted Solution

by:
ozo earned 900 total points
ID: 13898102
perldoc -q "How can I tell whether a certain element is contained in a list or array?"
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 900 total points
ID: 13899073
Although there is no way to do this without looping, you can loop only once, and then find multiple elements with no furthur loops

my %test;
@test{@test}=(1) x @test;

for( "visual","rahul" ){
   print "match $_\n" if $test{$_};
}

grep(/visual/, @arr) besides being slow, will also match if @arr contains somehing like "revisualizing"
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

809 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