in reply to creating hash of hashes from input file

#! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 50; $/ = ''; ## para mode; see Perlvar my %hash; while( <DATA> ) { my %subhash; for my $line ( split "\n" ) { my( $key, $value ) = split /\s+:\s+/, $line; $subhash{ $key } = $value; } $hash{ $subhash{ FullName } } = \%subhash; } pp \%hash; __DATA__ FullName : User1 Home Address : 111 address lane Phone : 555-555-5555 FullName : User2 Home Address : 222 address lane 2 Phone : 777-777-7777

Produces:

c:\test>junk7 { User1 => { FullName => "User1", "Home Address" => "111 address lane", Phone => "555-555-5555", }, User2 => { FullName => "User2", "Home Address" => "222 address lane 2", Phone => "777-777-7777", }, }

Replies are listed 'Best First'.
Re^2: creating hash of hashes from input file
by xiaoyafeng (Deacon) on Jul 17, 2010 at 14:47 UTC

    I'm glade to see my snippet is similar with BrowserUk's ;)

    Let me ask a question slightly OT:

    Why many monks don't like use parenthesis with functions such as split, map, print. is it because concise, readable or other reasons?





    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      Why many monks don't like use parenthesis with functions such as split, map, print. is it because concise, readable or other reasons?

      For the same reason most people don't type:

      my sum = (((($p+$q)+$r)+$s)+t);

      when

      my $sum = $p + $q + $r + $s + $t;

      is cleaner and clearer.

      Perhaps more realistically, my $z = sqrt( $x**2 + $y**2 );

      in preference to my $z = sqrt (($x**2)+($y**2));.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      is it because concise, readable or other reasons?
      Yes. Paraphrasing the Perl Best Practices book:
      • Reduces clutter
      • Enhances readability
      • Lack of parentheses visually distinguishes builtins from subroutine calls

      The book is full of well-articluated reasons for its many coding style recommendations.

        Lack of parentheses visually distinguishes builtins from subroutine calls

        Hm. Apart from the fact I don't need to use the addition or omission of parens to distinguish between built-ins and not; because in common with most (not all; but most), programmers I use a syntax highlighting editor that is capable of doing that for me.

        That distinction is wholly artificial.

        For starters, there are some places where the parens are not avoidable without jumping through contorted hoops that would completely negate any benefit of their omission. Eg.

        print '(' . join( ',', @things ) . ')'; ## (1,2,3,4,5,6,7,8,9,10) print '(' . join ',', @things . ')';; ## (10) Whoops! print '(', join ',', @things, ')';; ## ( 1,2,3,4,5,6,7,8,9,10,) Doub +le whoops!

        It also presupposes that everyone will always use, often redundant, parens on every non-built-in.

        And then the definition of what is a built-in and what is not gets blurred when you start using (for example),

        And at least one of those, behaves quite differently to the built-in in many circumstances, if used without parens.

        Also, I realise you're only paraphrasing, so the fault, (if there is one,) may be yours not the books, but aren't those first two "reasons" actually just one? Ie. Enhanced readability by reducing clutter. or By reducing clutter it enhances readability.

        I greatly prefer, and practice religiously, the "low clutter style" of Perl coding, and I think that the results are self-demonstrable. I don't see any additional benefit from overselling them with justifictions.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: creating hash of hashes from input file
by perlnewbie9292 (Novice) on Jul 17, 2010 at 15:21 UTC
    Thanks for the reply. I am not quite sure I follow everything you did in your code. So how do I access the elements without using pp \%hash? For example if I want output like shown here: With out the use of pp \%hash how to I print each element. Tried doing what I was doing before with a foreach but I just get the HASHREF# not the actual data.
    User User1 111 address lane 555-555-5555 User User2 222 address lane 2 777-777-7777

      #! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 50; $/ = ''; my %hash; while( <DATA> ) { my %subhash; for my $line ( split "\n" ) { my( $key, $value ) = split /\s+:\s+/, $line; $subhash{ $key } = $value; } $hash{ $subhash{ FullName } } = \%subhash; } ##pp \%hash; for my $primaryKey ( keys %hash ) { print "$primaryKey:"; for my $subKey ( keys %{ $hash{ $primaryKey } } ) { print "\t$subKey => $hash{ $primaryKey }{ $subKey }"; } } __DATA__ FullName : User1 Home Address : 111 address lane Phone : 555-555-5555 FullName : User2 Home Address : 222 address lane 2 Phone : 777-777-7777

      Gives:

      c:\test>junk7 User1: Phone => 555-555-5555 Home Address => 111 address lane FullName => User1 User2: Phone => 777-777-7777 Home Address => 222 address lane 2 FullName => User2
        Thanks just figured it out right before your post that I needed nested loops to actually get the data out. Thanks for the tips, I think need to play around more with hashes to get a better understanding. Thanks again for the quick responses. :)