?
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
?
252 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 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 84

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

649 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