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

I have a data file named "foo", whose contents is like this ...
IMNSearch.msg.en_US.rte.com:IMNSearch.msg.en_US.rte.com:2.4.0.0: : :C: + :Text Search Messages - U.S. English: : : : : : :1:0: IMNSearch.rte:IMNSearch.rte.com:2.4.0.0: : :C: :Text Search Client/Ser +ver Shared Files : : : : : : :1:0: IMNSearch.rte.httpdlite:IMNSearch.rte.httpdlite:2.1.0.0: : :C: :Lite N +etQuestion Local Web Server: : : : : : :1:0:
I want to read in this file, split each line on the ":", and store the values in a hash like ...
$data{"foo"}{IMNSearch.msg.en_US.rte.com} = 2.4.0.0
I think I've got this part working fine; the problem comes in when I try to print the output. What I wind up with is ...
IMNSearch.msg.en_US.rte.com IMNSearch.msg.en_US.rte.com IMNSearch.rte IMNSearch.rte IMNSearch.rte.httpdlite IMNSearch.rte.httpdlite
Here is what I've got ...
my (%data, $system, @fields); $system = "foo"; open (FILE, "$system"); while (<FILE>) { @fields = split (/:/, $_); $data{$system}{$fields[$1]} = $fields[$2]; }; close FILE; foreach (sort keys %{$data{$system}}) { printf ("%-30s%-30s\n", $_, $data{$system}{$_}); };
Am I missing something obvious, or doing something dumb (or both?)

Replies are listed 'Best First'.
Re: Help with printing values from a hash of hashes
by xdg (Monsignor) on Mar 31, 2006 at 02:26 UTC
    Am I missing something obvious, or doing something dumb (or both?)

    Unfortunately, I think so. You're using $1 and $2 when you probably just want 1 and 2.

    $data{$system}{$fields[$1]} = $fields[$2]; # problem here

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Thank you everyone ... now I truly feel like a moron. :-)
Re: Help with printing values from a hash of hashes
by roboticus (Chancellor) on Mar 31, 2006 at 02:35 UTC
    blueflashlight--

    You have extra '$' sigils where they're not wanted. Change the second line after the while statement to:

    $data{$system}{$fields[1]}=$fields[2];

    update: I guess I'll have to type faster! 8^) --roboticus

Re: Help with printing values from a hash of hashes
by BrowserUk (Patriarch) on Mar 31, 2006 at 02:26 UTC

    Replace

    $data{$system}{$fields[$1]} = $fields[$2];

    with

    $data{$system}{$fields[0]} = $fields[1];

    The $n variables are used to access data captured in regexes with capture brackets, not for indexing arrays.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      As I mention below, I think the OP wants indexes 1 and 2 -- meaning the 2nd and 3rd element after splitting on ":".

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Right. I just mentally translated first & second capture to first & second index, and didn't notice that the first field was repeated (in some cases and not quite in others), in the data.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.