Professional Web Applications Themes

Data::Dumper How to quote keys while dumping - PERL Miscellaneous

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

  1. #1

    Default Data::Dumper How to quote keys while dumping

    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 Guest

  2. #2

    Default Re: Data::Dumper How to quote keys while dumping

    kamal <kamalindia.ti.com> 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 Guest

  3. #3

    Default Re: Data::Dumper How to quote keys while dumping


    "kamal" <kamalindia.ti.com> wrote in message
    news:c34c425d.0308120401.49bac070posting.google.c om...
    > 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 ed 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 Guest

  4. #4

    Default Re: Data::Dumper How to quote keys while dumping

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "David K. Wall" <usenetdwall.fastmail.fm> wrote in
    news:Xns93D570BA9DA5Cdkwwashere216.168.3.30:
    > kamal <kamalindia.ti.com> 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/AwUBPzoKSmPeouIeTNHoEQJ3uACdGLGmSKbhbvYGIcxvMRg2Ff YzgE0An2BA
    XNFQYHdQh410yYg9vPGTkND7
    =xVhp
    -----END PGP SIGNATURE-----
    Eric J. Roode Guest

  5. #5

    Default Re: Data::Dumper How to quote keys while dumping

    Eric J. Roode <REMOVEsdnCAPScomcast.net> wrote:
    > "David K. Wall" <usenetdwall.fastmail.fm> wrote in
    > news:Xns93D570BA9DA5Cdkwwashere216.168.3.30:
    >
    >> kamal <kamalindia.ti.com> 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 Guest

  6. #6

    Default Re: Data::Dumper How to quote keys while dumping

    "James E Keenan" <jkeenconcentric.net> 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 Guest

Similar Threads

  1. Data::Dumper trouble
    By Jan Eden in forum PERL Beginners
    Replies: 2
    Last Post: February 13th, 12:52 PM
  2. Data::Dumper && null values
    By Sherm Pendley in forum PERL Modules
    Replies: 1
    Last Post: February 7th, 06:02 PM
  3. Data::Dumper->dump()ing a hash?
    By Dan Anderson in forum Perl / CGI
    Replies: 4
    Last Post: November 9th, 08:12 PM
  4. Data::Dumper vs perl -d
    By Randal L. Schwartz in forum PERL Miscellaneous
    Replies: 4
    Last Post: July 26th, 01:57 AM
  5. having keyboard problems with the quote keys and the ~ key
    By Jonathan Wilson in forum Linux Setup, Configuration & Administration
    Replies: 1
    Last Post: July 22nd, 01:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139