What's wrong with your "horrible" code is that you don't use the data in the outer loop. If written like that:

OUTER: foreach my $outerField (@fields[2,4,5,9,7,1]) { my %hash; INNER: foreach my $innerField(@fields[2,4,5,9,7,1]) { $hash{$innerField}++; } push @report, \%hash; }
you can see that $outerField is declared but never used. You're actually just doing six times the same thing in a row.

If I were you, I'd get the result of the split into a hash instead of an array, because selecting them by number doesn't help reading.

use strict; use warnings; use Data::Dumper; my %report; my $nameCount=0; my @names = map { $_.++$nameCount } ('Key', ) x 10; # Here the names will be Key1 to Key10, # But you should actually give explicit names with something like # my @names = qw/Princess Leia Obiwan Kenobi Anakin Skywalker Darth Va +der Han Solo/; while (my $line = <DATA>) { my %fields; @fields{@names} = (split / /, $line); # Here we put the result into + a slice of %fields foreach my $name (@names) { my $value = $fields{$name}; $report{$name}{$value}++; # Auto vivification here, $report{$nam +e} magically becomes a hashref } } print Dumper \%report; __DATA__ 483 OS dx-32 1 charles list4 aardvark.com ty-off lx-on C 01 483 DS dx-14 1 james list3 23.456.12.7 ty-on lx-on B 01 769 XO dx-32 5 sully nolist widgets.com ty-on lx-on V 07
$VAR1 = {
          'Key10' => {
                       'C' => 1,
                       'B' => 1,
                       'V' => 1
                     },
          'Key2' => {
                      'XO' => 1,
                      'OS' => 1,
                      'DS' => 1
                    },
          'Key1' => {
                      '483' => 2,
                      '769' => 1
                    },
          'Key5' => {
                      'james' => 1,
                      'sully' => 1,
                      'charles' => 1
                    },
          'Key8' => {
                      'ty-on' => 2,
                      'ty-off' => 1
                    },
          'Key4' => {
                      '1' => 2,
                      '5' => 1
                    },
          'Key6' => {
                      'nolist' => 1,
                      'list3' => 1,
                      'list4' => 1
                    },
          'Key3' => {
                      'dx-32' => 2,
                      'dx-14' => 1
                    },
          'Key7' => {
                      'aardvark.com' => 1,
                      'widgets.com' => 1,
                      '23.456.12.7' => 1
                    },
          'Key9' => {
                      'lx-on' => 3
                    }
        };
So if you want to access the information on the site name, and have named the seventh key "siteName", you just have to go for $report{siteName} instead of $report[7]

Appart from the use of a hash instead of an array, it's pretty much the same as LanX and BrotherUk's ones.


In reply to Re: array of hashes, categorized by array index by Eily
in thread array of hashes, categorized by array index by tevus_oriley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.