Is this type of referencing okay or insane, or is there mayber a simpler/better/faster way?

With some minor corrections and a pair of things I would do differently, it seems fine to me.

Update: here's one possible way I'd do it (note in particular that I don't use %index):

#!/usr/bin/perl use strict; use warnings; chomp (my $headerline = <DATA>); $headerline =~ s/^#// or die "Not a correct headerline"; my @keys=split /:/, $headerline; my @array; while (<DATA>) { chomp; my %hash; @hash{@keys}=split /:/; push @array, \%hash; } # examples print "1st name : $array[0]{'name'}\n"; print "3rd category : $array[2]{'category'}\n"; # traverse entire array for my $idx (0..$#array) { my $row=$idx+1; print "Row $row: ", (map "$_=$array[$idx]{$_} ", @keys), "\n"; } __DATA__ #name:surname:category tiger:woods:golfer tyler:hamilton:f1 james:stewart:supercross roger:federer:tennis

It seems simpler to me.


In reply to Re: text-file parsing with automatic field ordering by blazar
in thread text-file parsing with automatic field ordering by 0xbeef

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.