Data::Dumper How to quote keys while dumping

Discussion in 'Perl Misc' started by kamal, Aug 12, 2003.

  1. kamal

    kamal Guest

    I am trying to dump a hash using Data::Dumper.
    I need to quote the keys while dumping.

    My progarm looks like this

    use Data::Dumper;
    $Data::Dumper::Quotekeys = 1;
    $Data::Dumper::Useqq = 1;

    my %tmp ;

    $tmp{'1'} = "l";
    $tmp{'2'} = "u";

    open (STDOUT, ">");
    print STDOUT Data::Dumper->Dump([\%tmp], ['*tmp']);
    close STDOUT ;
    ---------------------------------------------------
    Output
    %tmp = (
    1 => "l",
    2 => "u"
    );

    ---------------------------------------

    i need it as

    %tmp = (
    '1' => "l",
    '2' => "u"
    );

    Can anyone help out with this

    Thanks
    Kamal
     
    kamal, Aug 12, 2003
    #1
    1. Advertising

  2. kamal <> wrote:

    > I am trying to dump a hash using Data::Dumper.
    > I need to quote the keys while dumping.
    >
    > My progarm looks like this
    >
    > use Data::Dumper;
    > $Data::Dumper::Quotekeys = 1;
    > $Data::Dumper::Useqq = 1;
    >
    > my %tmp ;
    >
    > $tmp{'1'} = "l";
    > $tmp{'2'} = "u";
    >
    > open (STDOUT, ">");


    You should always check to see if open() succeeded:

    open (STDOUT, ">") or die "Cannot open STDOUT: $!";

    Wy are you opening STDOUT, anyway? It's already available for use
    unless you did something to it.


    > print STDOUT Data::Dumper->Dump([\%tmp], ['*tmp']);


    Quoting is the default. Don't give Dump() names if that's not what
    you want.

    print STDOUT Data::Dumper->Dump([\%tmp]);

    I'm lazy, so I'd write this as

    print Dumper \%tmp;


    > close STDOUT ;



    --
    David Wall
     
    David K. Wall, Aug 12, 2003
    #2
    1. Advertising

  3. "kamal" <> wrote in message
    news:...
    > My progarm looks like this
    >
    > use Data::Dumper;
    > $Data::Dumper::Quotekeys = 1;
    > $Data::Dumper::Useqq = 1;
    >
    > my %tmp ;
    >
    > $tmp{'1'} = "l";
    > $tmp{'2'} = "u";
    > [snip]
    > ---------------------------------------------------
    > Output
    > %tmp = (
    > 1 => "l",
    > 2 => "u"
    > );
    >
    > ---------------------------------------
    >
    > i need it as
    >
    > %tmp = (
    > '1' => "l",
    > '2' => "u"
    > );
    >


    David Wall's points about how needlessly complicated your code is are well
    taken. However, the particular thing that screwed you up, AFAICT, was:
    $Data::Dumper::Useqq = 1;
    This outputs numerical keys unquoted. Apparently Perl treats these as
    numbers rather than strings. Simply omitting Useqq should solve your
    problem. Cf:
    use Data::Dumper;
    my %tmp ;
    $tmp{'1'} = "l";
    $tmp{'2'} = "u";
    $tmp{'alpha'} = "a";
    $tmp{'beta'} = "b";

    print Dumper(\%tmp);
    print "\n";
    {
    $Data::Dumper::Useqq = 1;
    print Dumper(\%tmp);
    }
    I think this is closer to what you want.
    jimk
     
    James E Keenan, Aug 13, 2003
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "David K. Wall" <> wrote in
    news:Xns93D570BA9DA5Cdkwwashere@216.168.3.30:

    > kamal <> wrote:
    >
    >> print STDOUT Data::Dumper->Dump([\%tmp], ['*tmp']);

    >
    > Quoting is the default. Don't give Dump() names if that's not what
    > you want.
    >
    > print STDOUT Data::Dumper->Dump([\%tmp]);
    >
    > I'm lazy, so I'd write this as
    >
    > print Dumper \%tmp;


    Passing a second argument to Dump has nothing to do with quoting, unless
    I'm mistaken...right? Your two shortcuts would produce a string that began
    with

    $VAR1 = {....

    rather than

    %tmp = (....

    as the OP wanted.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPzoKSmPeouIeTNHoEQJ3uACdGLGmSKbhbvYGIcxvMRg2FfYzgE0An2BA
    XNFQYHdQh410yYg9vPGTkND7
    =xVhp
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Aug 13, 2003
    #4
  5. Eric J. Roode <> wrote:

    > "David K. Wall" <> wrote in
    > news:Xns93D570BA9DA5Cdkwwashere@216.168.3.30:
    >
    >> kamal <> wrote:
    >>
    >>> print STDOUT Data::Dumper->Dump([\%tmp], ['*tmp']);

    >>
    >> Quoting is the default. Don't give Dump() names if that's not
    >> what you want.
    >>
    >> print STDOUT Data::Dumper->Dump([\%tmp]);
    >>
    >> I'm lazy, so I'd write this as
    >>
    >> print Dumper \%tmp;

    >
    > Passing a second argument to Dump has nothing to do with quoting,
    > unless I'm mistaken...right? Your two shortcuts would produce a
    > string that began with
    >
    > $VAR1 = {....
    >
    > rather than
    >
    > %tmp = (....
    >
    > as the OP wanted.


    Yup, you're right. Sometimes I wonder why I even test code before I
    post....


    --
    David Wall
     
    David K. Wall, Aug 13, 2003
    #5
  6. "James E Keenan" <> wrote in message news:<bhc9b0> use Data::Dumper;
    > my %tmp ;
    > $tmp{'1'} = "l";
    > $tmp{'2'} = "u";
    > $tmp{'alpha'} = "a";
    > $tmp{'beta'} = "b";
    >
    > print Dumper(\%tmp);
    > print "\n";
    > {
    > $Data::Dumper::Useqq = 1;
    > print Dumper(\%tmp);
    > }


    To be more precise, for that last block I should have written:

    {
    local $Data::Dumper::Useqq = 1;
    print Dumper(\%tmp);
    }
     
    James E Keenan, Aug 13, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kamal
    Replies:
    0
    Views:
    986
    kamal
    Aug 12, 2003
  2. Replies:
    3
    Views:
    13,237
  3. Stimp
    Replies:
    2
    Views:
    2,269
    Stimp
    Sep 20, 2006
  4. Replies:
    4
    Views:
    249
    Paul Lalli
    Aug 1, 2007
  5. John
    Replies:
    0
    Views:
    96
Loading...

Share This Page