print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

[[v5.8.8 built for x86_64-linux-thread-multi]
#!/usr/bin/perl -W
use Getopt::Long;
my $dml = 0;
my $iterations = 10;

my %options = ("dml!" => \$dml,
"iterations=i" => \$iterations);
GetOptions(%options) || die "bad options";
printf "dml=$dml\n";
print %options;
foreach $key (sort keys %options) {
printf "$key: $options{$key}\n";}

This script prints:
dml=0
dml!SCALAR(0xa461540)iterations=iSCALAR(0xa461560)dml!:
SCALAR(0xa461540)
iterations=i: SCALAR(0xa461560)

How can I get readable output of my getopt options without manually
enumerating them?


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
gry [ Mi, 09 Februar 2011 22:52 ] [ ID #2054939 ]

Re: print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

On 11-02-09 04:52 PM, gry wrote:
> [[v5.8.8 built for x86_64-linux-thread-multi]
> #!/usr/bin/perl -W
> use Getopt::Long;
> my $dml = 0;
> my $iterations = 10;
>
> my %options = ("dml!" => \$dml,
> "iterations=i" => \$iterations);
> GetOptions(%options) || die "bad options";
> printf "dml=$dml\n";
> print %options;
> foreach $key (sort keys %options) {
> printf "$key: $options{$key}\n";}
>
> This script prints:
> dml=0
> dml!SCALAR(0xa461540)iterations=iSCALAR(0xa461560)dml!:
> SCALAR(0xa461540)
> iterations=i: SCALAR(0xa461560)
>
> How can I get readable output of my getopt options without manually
> enumerating them?
>
>

With Getyopt::Long, the value of the options is in the variable:

print "\$dml = $dml\n";
print "\iterations = $iterations\n";


--
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.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Do, 10 Februar 2011 02:13 ] [ ID #2054941 ]

Re: print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

gry wrote:
> [[v5.8.8 built for x86_64-linux-thread-multi]
> #!/usr/bin/perl -W
> use Getopt::Long;
> my $dml = 0;
> my $iterations = 10;
>
> my %options = ("dml!" => \$dml,
> "iterations=i" => \$iterations);
> GetOptions(%options) || die "bad options";
> printf "dml=$dml\n";

That should be either:

print "dml=$dml\n";

Or:

printf "dml=%s\n", $dml;


> print %options;
> foreach $key (sort keys %options) {
> printf "$key: $options{$key}\n";}

That should be either:

print "$key: $options{$key}\n";}

Or:

printf "%s: %s\n", $key, $options{$key};}

$options{$key} contains a reference to a scalar so you have to
dereference it:

print "$key: ${$options{$key}}\n";}

Or:

printf "%s: %s\n", $key, ${$options{$key}};}


> This script prints:
> dml=0
> dml!SCALAR(0xa461540)iterations=iSCALAR(0xa461560)dml!:
> SCALAR(0xa461540)
> iterations=i: SCALAR(0xa461560)
>
> How can I get readable output of my getopt options without manually
> enumerating them?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Do, 10 Februar 2011 04:31 ] [ ID #2054943 ]

Re: print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

On Feb 9, 8:13=A0pm, shawnhco... [at] gmail.com (Shawn H Corey) wrote:
> On 11-02-09 04:52 PM, gry wrote:
>
>
>
> > [[v5.8.8 built for x86_64-linux-thread-multi]
> > #!/usr/bin/perl -W
> > use Getopt::Long;
> > my $dml =3D 0;
> > my $iterations =3D 10;
>
> > my %options =3D ("dml!" =3D> =A0\$dml,
> > =A0 =A0 =A0 =A0 =A0 "iterations=3Di" =3D> =A0\$iterations);
> > GetOptions(%options) || die "bad options";
> > printf "dml=3D$dml\n";
> > print %options;
> > foreach $key (sort keys %options) {
> > =A0 =A0 =A0printf "$key: $options{$key}\n";}
>
> > This script prints:
> > dml=3D0
> > dml!SCALAR(0xa461540)iterations=3DiSCALAR(0xa461560)dml!:
> > SCALAR(0xa461540)
> > iterations=3Di: SCALAR(0xa461560)
>
> > How can I get readable output of my getopt options without manually
> > enumerating them?
>
> With Getopt::Long, the value of the options is in the variable:
>
> print "\$dml =3D $dml\n";
> print "\iterations =3D $iterations\n";
Thanks, Shawn.
I understand that I can explicitly print each variable, as you show.
But what I want to do is iterate through the hash, printing
"name=3Dvalue" for each option. Otherwise, when I add/delete/change
options, I would have to reflect this in the printing statements.
Is there some way to dereference these "SCALAR" things, or a better
way to refer to the values from the hash?
>
> --
> Just my 0.00000002 million dollars worth,
> =A0 =A0Shawn


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
gry [ Do, 10 Februar 2011 03:41 ] [ ID #2054953 ]

Re: print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

On 09/02/2011 21:52, gry wrote:
> [[v5.8.8 built for x86_64-linux-thread-multi]
> #!/usr/bin/perl -W
> use Getopt::Long;
> my $dml = 0;
> my $iterations = 10;
>
> my %options = ("dml!" => \$dml,
> "iterations=i" => \$iterations);
> GetOptions(%options) || die "bad options";
> printf "dml=$dml\n";
> print %options;
> foreach $key (sort keys %options) {
> printf "$key: $options{$key}\n";}
>
> This script prints:
> dml=0
> dml!SCALAR(0xa461540)iterations=iSCALAR(0xa461560)dml!:
> SCALAR(0xa461540)
> iterations=i: SCALAR(0xa461560)
>
> How can I get readable output of my getopt options without manually
> enumerating them?

You are confused and using a mixture of two different ways of specifying
the expected parameters. If you want the values in a hash then you
should code as below.

Please /always/ use strict.

HTH,

Rob


use strict;
use warnings;

use Getopt::Long;

my %options = (
dml => 0,
iterations => 10,
);

GetOptions \%options, 'dml!', 'iterations=i' or die 'Bad command line
options';

foreach my $key (sort keys %options) {
print "$key: $options{$key}\n";
}

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 11 Februar 2011 07:19 ] [ ID #2055015 ]
Perl » gmane.comp.lang.perl.beginners » print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"

Vorheriges Thema: disable submit button when page refresh with back button.
Nächstes Thema: return ()