A better way to add to a hash?

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jan 21, 2006.

  1. Guest

    Kindly consider this code which builds a hash from associated __DATA__
    pairs:

    #!/usr/bin/perl
    use warnings; use strict;
    use Data::Dumper;

    my %setting;
    foreach my $pair(<DATA>) {
    $setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
    }

    print Dumper(\%setting); #yeah, it works, but...

    __DATA__
    user=root
    password=easy
    hostname=abcxyz

    This works. But I'm not really happy with using a "for" loop like that
    - it just doesn't seem like the best way to go (but I can't really
    explain why; I just kinda linger there and stare at it when I mentally
    parse the code).

    Is there a better way to do this than using the "for" loop that way?

    Thanks!

    --
    http://DavidFilmer.com
     
    , Jan 21, 2006
    #1
    1. Advertising

  2. wrote:
    > Kindly consider this code which builds a hash from associated __DATA__
    > pairs:
    >
    > #!/usr/bin/perl
    > use warnings; use strict;
    > use Data::Dumper;
    >
    > my %setting;
    > foreach my $pair(<DATA>) {
    > $setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
    > }
    >
    > print Dumper(\%setting); #yeah, it works, but...
    >
    > __DATA__
    > user=root
    > password=easy
    > hostname=abcxyz
    >
    > This works. But I'm not really happy with using a "for" loop like that
    > - it just doesn't seem like the best way to go (but I can't really
    > explain why; I just kinda linger there and stare at it when I mentally
    > parse the code).
    >
    > Is there a better way to do this than using the "for" loop that way?


    Maybe not "better".

    while ( <DATA> ) {
    @setting{ /(.*)=/ } = /=(.*)/;
    }


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jan 21, 2006
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Kindly consider this code which builds a hash from associated __DATA__
    > pairs:
    >
    > #!/usr/bin/perl
    > use warnings; use strict;
    > use Data::Dumper;
    >
    > my %setting;
    > foreach my $pair(<DATA>) {
    > $setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
    > }
    >
    > print Dumper(\%setting); #yeah, it works, but...
    >
    > __DATA__
    > user=root
    > password=easy
    > hostname=abcxyz
    >
    > This works. But I'm not really happy with using a "for" loop like that
    > - it just doesn't seem like the best way to go (but I can't really
    > explain why; I just kinda linger there and stare at it when I mentally
    > parse the code).


    One problem is to get rid of the newlines.

    my @setting = map split( /[=\n]), <DATA>;
    or
    my %setting = map split( /[=]/), map /(.*)/, <DATA>;;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Jan 21, 2006
    #3
  4. Xicheng Guest

    wrote:
    > Kindly consider this code which builds a hash from associated __DATA__
    > pairs:
    >
    > #!/usr/bin/perl
    > use warnings; use strict;
    > use Data::Dumper;
    >
    > my %setting;
    > foreach my $pair(<DATA>) {
    > $setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]


    $pair =~ m{(.*)=(.*)} and $setting{$1} = $2;

    > }
    >
    > print Dumper(\%setting); #yeah, it works, but...
    >
    > __DATA__
    > user=root
    > password=easy
    > hostname=abcxyz
    >
    > This works. But I'm not really happy with using a "for" loop like that
    > - it just doesn't seem like the best way to go (but I can't really
    > explain why; I just kinda linger there and stare at it when I mentally
    > parse the code).
    >
    > Is there a better way to do this than using the "for" loop that way?
    >
    > Thanks!
    >
    > --
    > http://DavidFilmer.com
     
    Xicheng, Jan 21, 2006
    #4
  5. wrote in news:1137831430.317899.236140
    @f14g2000cwb.googlegroups.com:

    > Kindly consider this code which builds a hash from associated __DATA__
    > pairs:
    >
    > #!/usr/bin/perl
    > use warnings; use strict;
    > use Data::Dumper;
    >
    > my %setting;
    > foreach my $pair(<DATA>) {


    Well, first of all, I would stick with reading the file line-by-line
    when processing is to be done line-by-line. I know it doesn't matter
    much in this case, but it is still a good habit to get into.


    > $setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
    > }


    Second, I am just too conservative I guess.

    Also, I think the regex is too permissive. Are there really no
    restrictions on the keys of the hash?

    If the settings are being read from an external file, I would prefer to
    initialize the hash with the keys that are acceptable, and only set
    those.

    Given the possibility of further restrictions on keys and/or values, I
    would prefer the more boring, but easier to read (to me) and extend form
    below.

    #!/usr/bin/perl
    use warnings; use strict;
    use Data::Dumper;

    my %setting = (
    user => 'default_user',
    password => undef,
    hostname => 'default_host',
    );

    while(<DATA>) {
    if( m{\A (\w+) \s* = \s* ([^\n]+) \n \z }xms ) {
    if( exists $setting{$1} ) {
    $setting{$1} = $2;
    }
    }
    }

    print Dumper(\%setting);

    __DATA__
    user = root
    password = easy
    hostname = abcxyz
    empty =
    bogus = ewhfljeh lksadfj sadkjf sadlkfj

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jan 21, 2006
    #5
    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. rp
    Replies:
    1
    Views:
    580
    red floyd
    Nov 10, 2011
  2. skibud2
    Replies:
    6
    Views:
    172
    Robert Klemme
    Aug 1, 2007
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    668
    David A. Black
    Jul 2, 2008
  4. mike solomon

    better way of building string from hash

    mike solomon, Jul 17, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    113
    Uri Guttman
    Jul 17, 2003
  5. Replies:
    2
    Views:
    66
    Mark H Harris
    May 13, 2014
Loading...

Share This Page