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
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Accepted Solution

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

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

Assisted Solution

by:FishMonger
FishMonger earned 250 total points
ID: 39192860
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
ID: 39192870
Can you clarify what you mean by "has"?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 174
Which version of C should I use when Perl is too slow? 13 197
crawling gofundme 4 150
Regex rule to match two different url 5 77
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

739 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