[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
253 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 85

Accepted Solution

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

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

Assisted Solution

by:FishMonger
FishMonger earned 1000 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 85

Expert Comment

by:ozo
ID: 39192870
Can you clarify what you mean by "has"?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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…
Six Sigma Control Plans
Suggested Courses

872 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