Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting an Error message with this code (Both attached below) and i'm going insane trying to spot what i've done wrong. I'm going to use this as a base configuration file if it ever works.... Error Message: Odd number of elements in hash assignment at dumper2 line 5 #! /usr//bin/perl use strict; use Data::Dumper; my %Config =( PPingPath => '/home/dean/pping-v1.37/pping ', PPingOptions => '-t 15 -q |', TraceRTPath => '/usr/sbin/traceroute', TRHops => '-m 3 | ', ExternalTarget => ('www.microsoft.com -p 80', 'www.netscape.com -p 80', 'www.silverhand.co.uk -p 80', 'www.demon.net -p 80', 'www.whitehouse.org -p 80'), Hosts => ('www.netscape.come -p 80', 'www.microsoft.com -p 80') ); my $Dumperl1 = Data::Dumper->new( %Config ); my $DumpedPerl1 = $Dumperl1->Dump(); print $DumpedPerl1; exit; Code:
  • Comment on Odd number of elements in hash assignment

Replies are listed 'Best First'.
Re: Odd number of elements in hash assignment
by chromatic (Archbishop) on Jun 07, 2000 at 19:50 UTC
    The error only comes about because there are 17 items altogether in your hash assignment. If you had one more URL, for example, you'd be stuck with a hard-to-find error. You're lucky.

    If you mean to store a list in a hash, you'll have to create a reference, either by creating an array and storing a reference to it as the value in the hash, or making an anonymous list right there. Let's take the latter approach:

    my %Config = ( PPingPath => '/home/dean/pping-v1.37/pping', PPingOptions => '-t 15 -q |', TraceRTPath => '/usr/sbin/traceroute', TRHops => '-m 3 | ', ExternalTarget => [ 'www.microsoft.com -p 80', 'www.netscape.com -p 80', 'www.silverhand.co.uk -p 80', 'www.demon.net -p 80', 'www.whit +ehouse.org -p 80'], Hosts => [ 'www.netscape.come -p 80', 'www.microsoft.com -p 80'] );
    See perlref for more information. (And remember that a hash can only store scalar values -- as a reference can be stored in a scalar, you can build more complex data structures by references. perldsc is the next place to look.)
RE: Odd number of elements in hash assignment
by Anonymous Monk on Jun 07, 2000 at 23:19 UTC
    Other posters have basically told you what you need to do differently, but I just wanted to be sure you understood why:
    my %Config =( PPingPath => '/home/dean/pping-v1.37/pping ', PPingOptions => '-t 15 -q |', TraceRTPath => '/usr/sbin/traceroute', TRHops => '-m 3 | ', ExternalTarget => ( 'www.microsoft.com -p 80', 'www.netscape.com -p 80', 'www.silverhand.co.uk -p 80', 'www.demon.net -p 80', 'www.whitehouse.org -p 80' ), Hosts => ( 'www.netscape.come -p 80', 'www.microsoft.com -p 80' ) );
    This is exactly equivalent to:
    my %Config =( 'PPingPath', '/home/dean/pping-v1.37/pping ', 'PPingOptions', '-t 15 -q |', 'TraceRTPath', '/usr/sbin/traceroute', 'TRHops', '-m 3 | ', 'ExternalTarget', 'www.microsoft.com -p 80', 'www.netscape.com -p 80', 'www.silverhand.co.uk -p 80', 'www.demon.net -p 80', 'www.whitehouse.org -p 80', 'Hosts', 'www.netscape.come -p 80', 'www.microsoft.com -p 80' );
    Since the parenthesis in there the way you used them essentially just *grouping* the items, not indicating that they're an array reference, so they don't really have an effect. Note the number of arguments above are odd, which isn't valid in a hash assignment (each of the "keys" on the left and the "values" on the right), so you get that error. If you want to use a list as a "value" for
RE: Odd number of elements in hash assignment
by jjhorner (Hermit) on Jun 07, 2000 at 20:08 UTC

    Okay, I don't know what you are trying to do, but this may get you closer:

    #!/usr//bin/perl use strict; use Data::Dumper; my %Config =( PPingPath => '/home/dean/pping-v1.37/pping ', PPingOptions => '-t 15 -q |', TraceRTPath => '/usr/sbin/traceroute', TRHops => '-m 3 | ', ExternalTarget => q('www.microsoft.com -p 80', 'www.ne +tscape.com -p 80', 'www.silverhand.co.uk -p 80', 'www.demon.net -p 80','www.whitehouse.org -p 80'), Hosts => q('www.netscape.come -p 80', 'www.microsoft.c +om -p 80') ); my @keys = (keys %Config); my @values = (values %Config); my $Dumperl1 = Data::Dumper->new( \@values, \@keys ); print $Dumperl1->Dump(); exit;

    Explanation:

    Data::Dumper->new takes an array reference (or two) as arguments, not a hash. I suppose it would work like this:

    my $Dumperl1 = Data::Dumper->new(\(keys %Config), \(values %Config));

    UPDATE:

    The above full program yields:

    $Hosts = '\'www.netscape.come -p 80\', \'www.microsoft.com -p 80\''; $ExternalTarget = '\'www.microsoft.com -p 80\', \'www.netscape.com -p +80\', \'www.silverhand.co.uk -p 80\', \'www.demon.net -p 80\',\'www.w +hitehouse.org -p 80\''; $TraceRTPath = '/usr/sbin/traceroute'; $PPingOptions = '-t 15 -q |'; $TRHops = '-m 3 | '; $PPingPath = '/home/dean/pping-v1.37/pping ';
    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/