Solved

How to find the the indice number of the array that has a given string in it in Perl?

Posted on 2013-05-23
3
244 Views
Last Modified: 2013-05-29
Hi,
I have an array called @data.

In this array, I am looking for $line.

How can I find the the indice number of the array that has $line in it in an efficient way?

Thanks,
0
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
Comment Utility
@indice{@data}=(0..$#data);

for $line ( @lines ){
   print $number = $indice{$line};
}
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 250 total points
Comment Utility
use List::MoreUtils qw(indexes first_index);

my @list = (1, 4, 3, 2, 4, 6);

my $first_index = first_index {/4/} @list;
my @all_indexes = indexes {/4/} @list;

print "first index:$first_index\n";
print "all indexes:@all_indexes\n";

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Can you clarify what you mean by "has"?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now