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

2011-02-10 Thread gry
On Feb 9, 8:13 pm, shawnhco...@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 = 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 Getopt::Long, the value of the options is in the variable:

 print \$dml = $dml\n;
 print \iterations = $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=value 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.0002 million dollars worth,
    Shawn


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




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

2011-02-10 Thread Rob Dixon

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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




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

2011-02-09 Thread Shawn H Corey

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.0002 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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




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

2011-02-09 Thread John W. Krahn

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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/