开发者

How to use GetOptions in hash array?

I used to 'GetOptions' in hash arrays.

I have a hash data structure below.

%fruit=(
    banana => [ 1, "yellow" ],
    cherry => [ 2, "dark red" ],
    strawberry => [ 3, "r开发者_Python百科ed" ],
);

The key is name of fruits.

Then I want to use 'GetOptions' for fruit 'name', 'number' and 'color'.

Can you please let me know how to use GetOptions...?

I want to use like -n for searching name and -i for number and -c for color.

Please let me know... :(


I don't think you're quite understanding what GetOptions does.

It parses the options you passed in.

So for your example:

> myprog.pl --name banana 

In your program you would have:

my $name;
GetOptions("name=s" => \$name);

Your $name will then contain banana

You would then need to get the name from that variable, extract the matching entry from your %fruit hash, and output the results.


%fruit is a hash, but the values in the hash are REFERENCES to a two member array.

What GetOptions does is take an array (such as %ARGV) and parses the options out of that array. You can put those options into scalars, arrays, and even hashes, but There's no direct way of saying that a command line option is a reference to an array.

The best you're going to be able to do is create an array that will contain that values:

$ myprogram --fruit banana=yellow --fruit cherry="dark red" --fruit strawberry=red

Then you can have:

 GetOptions('fruit=s' => \@fruitHash);

The array will look like this:

$fruit[0] = 'banana=yellow';
$fruit[1] = 'cherry=dark red';
$fruit[2] = 'strawberry=red';

From there, you could go through the array and create your hash reference:

my %fruitHash;
my $count = 1;
foreach my $value (@fruit) {
    my ($fruit, $color) = split(/=/, $value);
    $fruit{$fruit}=>[0] = $count,
    $fruit{$fruit)=>[1] = $color,
}

There's no way to get GetOptions to understand that the command line options are a reference to a two member array.


You did no accept an answer. So may be this one fits.

Because you did not provide examples how to call your script, I assume something like this:

test.pl -i 3

gives us

strawberry, 3, red

and

test.pl -n cherry

gives us

cherry, 2, dark red

and

test.pl - c yellow

gives us

banana, 1, yellow


Use the following script:

#!/usr/local/bin/perl -w

use strict;
use warnings;

use Getopt::Std;
use vars qw (%opt);

getopts( "i:n:c:", \%opt ) or usage();

use Data::Dumper;
my %fruit=(
    banana => [ 1, "yellow" ],
    cherry => [ 2, "dark red" ],
    strawberry => [ 3, "red" ],
);

if ($opt{n}) {
   print "Fruit: $opt{n}, $fruit{$opt{n}}[0], $fruit{$opt{n}}[1] \n";
} elsif ($opt{i}) {
   while( my ($key, $value) = each %fruit) {
      if ($opt{i} == $value->[0] ) {
         print "Fruit: $key, $value->[0], $value->[1] \n";
         last;
      }
   }
} elsif ($opt{c}) {
   while( my ($key, $value) = each %fruit) {
      if ($opt{c} eq $value->[1] ) {
         print "Fruit: $key, $value->[0], $value->[1] \n";
         last;
      }
   }
}

If that is not what you need, tell us more!


I'm trying to use getoptions for name , number and color searches. For example type '-n' with name like 'banana' in command line, then searchinng banana=>[1, "yellow"]

That's not your question. This is quite simple:

use GetOpt::Long;


my ($number, $name, $color);
my $results = GetOptions (
    "number=i" => \$number,
    "name=s"   => \$name,
    "color=s"  => \$color,
);

Then, check the values of $number, $name, and $color. (Also check $results and give a nice error message if GetOptions returns a false value);

my $numOfParams = 0;
if ($color)  {$numOfParams++};
if ($number) {$numOfParams++};
if ($name)   {$numOfParams++};

die qq(Too many options given\n) if ($numOfParams > 1);
die qq(You didn't give any parameters\n) #'
    if ($NumOfParams == 0);

if ($color) {
    searchOnColor($color);
} elsif ($number) {
    searchOnNumber($number);
} elsif ($name) {
    searchOnName($name);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜