Comparison Value

T

ThERiZla

Hi,

I want find in an array a value start by this character : "-".
This is two examples of value in my array, I want find: -c, -s .
I use a while to scan my array, but I don't know how compare my values to find
the good one.

Suggestions and/or examples anyone ?

Thanks
 
G

Gunnar Hjalmarsson

ThERiZla said:
I want find in an array a value start by this character : "-".
This is two examples of value in my array, I want find: -c, -s .
I use a while to scan my array,

Do you? How do you do that?
but I don't know how compare my values to find the good one.

push @values, $_ if substr($_, 0, 1) eq '-';
 
J

Jay Tilton

(e-mail address removed) (ThERiZla) wrote:

: I want find in an array a value start by this character : "-".
: This is two examples of value in my array, I want find: -c, -s .
: I use a while to scan my array, but I don't know how compare my values to find
: the good one.

Sounds like you're trying to recognize switches passed in from the command
line. The Getopt::Std module would be useful.
 
P

pkent

I want find in an array a value start by this character : "-".
This is two examples of value in my array, I want find: -c, -s .
I use a while to scan my array, but I don't know how compare my values to find
the good one.

Suggestions and/or examples anyone ?

my @foo = qw(-c cheap as -s chips);
foreach (@foo) {
if (index($_, '-') == 0) {
print "Found the item $_\n";
# or do whatever else you want...
}
}
__END__

That prints:

Found the item -c
Found the item -s

on my machine.

[ But as pointed out by another poster, if you're parsing command line
options you should use Getopt::Std or Getopt::Long unless you have a
_really_ good reason not to (e.g. you're trying to implement a drop-in
replacement for something with unusual option syntax, such as tar(1) or
the much more unusual usfdump(1M)) ]

P
 
A

Anno Siegel

ThERiZla said:
Hi,

I want find in an array a value start by this character : "-".
This is two examples of value in my array, I want find: -c, -s .
I use a while to scan my array, but I don't know how compare my values to find
the good one.

Suggestions and/or examples anyone ?

my @good_ones = grep /^-/, @array;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top