trouble derefencing

trouble derefencing

am 08.10.2011 20:37:50 von Natxo Asenjo

hi,

I need to add a cli option to my script to push multiple values in a
hash. I have verified that using Getopt::Long this is possible, but
cannot get my head around dereferencing it.

This is a sample code:

============================================================ ==
use strict;
use warnings;
use Getopt::Long;

Getopt::Long::Configure( "no_ignore_case", "bundling" );

my ($checknow, $help, $revision, %excl_ref );
GetOptions(
'c|checknow' => \$checknow,
'h|help|?' => \$help,
'V|version' => \$revision,
'e|exclude=s%' => sub { push( @{$excl_ref{$_[1]}}, $_[2] ) },
);

use Data::Dumper;

print Dumper %excl_ref;
============================================================ =

when I run this I get:
../kk.pl -e test=1 -e test=2 -e test=3
$VAR1 = 'test';
$VAR2 = [
'1',
'2',
'3'
];

But I do not see how I can retrieve these values. I understand that in
Getopt::Long when we use a sub, the first argument $_[0] is the name
of the option, $_[1] in this case is the key and $_[2] is the value,
but I do not understand how I can dereference it, I am afraid.

Any help appreciated. TIA.
--
Groeten,
natxo

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: trouble derefencing

am 08.10.2011 20:47:15 von John SJ Anderson

On Sat, Oct 8, 2011 at 14:37, Natxo Asenjo wrote:
> hi,
>
> I need to add a cli option to my script to push multiple values in a
> hash. I have verified that using Getopt::Long this is possible, but
> cannot get my head around dereferencing it.
[ snip ]
> when I run this I get:
> ./kk.pl -e test=3D1 -e test=3D2 -e test=3D3
> $VAR1 =3D 'test';
> $VAR2 =3D [
>          '1',
>          '2',
>          '3'
>        ];

%excl_ref is a hash where the keys are on the left hand side of the
'=3D' and the values are arrayrefs of the things on the right hand side
of the '=3D'.

This will be more clear if you pass Data::Dumper a reference; that
will get you output that looks like this:

$ ~/try.pl -e test=3D1 -e test=3D2 -e test=3D3
$VAR1 =3D {
'test' =3D> [
'1',
'2',
'3'
]
};

You could access those -- depending on what you're trying to do -- like so:

foreach my $key ( keys %excl_ref ) {
foreach my $value ( @{ $excl_ref{$key} ) {
print "$key =3D $value\n";
}
}

Hope this helps.

chrs,
john.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: trouble derefencing

am 08.10.2011 20:47:33 von Shawn H Corey

On 11-10-08 02:37 PM, Natxo Asenjo wrote:
> hi,
>
> I need to add a cli option to my script to push multiple values in a
> hash. I have verified that using Getopt::Long this is possible, but
> cannot get my head around dereferencing it.
>
> This is a sample code:
>
> ============================================================ ==
> use strict;
> use warnings;
> use Getopt::Long;
>
> Getopt::Long::Configure( "no_ignore_case", "bundling" );
>
> my ($checknow, $help, $revision, %excl_ref );
> GetOptions(
> 'c|checknow' => \$checknow,
> 'h|help|?' => \$help,
> 'V|version' => \$revision,
> 'e|exclude=s%' => sub { push( @{$excl_ref{$_[1]}}, $_[2] ) },
> );
>
> use Data::Dumper;
>
> print Dumper %excl_ref;

print Dumper \%excl_ref;

> ============================================================ =
>
> when I run this I get:
> ./kk.pl -e test=1 -e test=2 -e test=3
> $VAR1 = 'test';
> $VAR2 = [
> '1',
> '2',
> '3'
> ];

%excl_ref has only one key 'test' and its value is an anonymous hash [
'1', '2', '3' ]

When you don't send the reference to Dumper(), it expands the hash to a
list and dumps each item in the list.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/