Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

text-file parsing with automatic field ordering

by 0xbeef (Hermit)
on May 31, 2007 at 10:51 UTC ( #618456=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed monks,

I have somehow managed to produce the following code (derived in part from Using variable names from an array), which uses the first commented line of an input file to determine the order of the data records that follow.

This means I do not have to make code changes when the file layout changes since I can simply adjust the commented header according the new data layout. i.e. a new file format of #category:surname:name will just automagically work...

#!/usr/bin/perl -w use strict; my %index; my @array; my $headerline = <DATA>; chomp $headerline; $headerline =~ s/^#//; my @header = split /:/,$headerline; @index{@header} = (0..$#header); while (<DATA>) { chomp; my @fields = split /:/,$_; for my $key (keys %index) { $array[$. - 1]{$key} = $fields[$index{$key}]; } } # examples print "1st name : $array[1]{'name'}\n"; print "3rd category : $array[3]{'category'}\n"; # traverse entire array for my $row (1..$#array) { print "Row $row: "; for my $keyname (keys %index) { print "$keyname=$array[$row]{$keyname} "; } print "\n"; } __DATA__ #name:surname:category tiger:woods:golfer tyler:hamilton:f1 james:stewart:supercross roger:federer:tennis

which produces

1st name : tiger 3rd category : supercross Row 1: name=tiger category=golfer surname=woods Row 2: name=tyler category=f1 surname=hamilton Row 3: name=james category=supercross surname=stewart Row 4: name=roger category=tennis surname=federer
Is this type of referencing okay or insane, or is there mayber a simpler/better/faster way? (First prize = without using any external module)

I do understand that the script lacks some error handling, but the question is more about the referencing...

Niel

Replies are listed 'Best First'.
Re: text-file parsing with automatic field ordering
by blazar (Canon) on May 31, 2007 at 11:27 UTC
    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):

    It seems simpler to me.

Re: text-file parsing with automatic field ordering
by Fletch (Bishop) on May 31, 2007 at 13:14 UTC

    You might be interested in Tie::Handle::CSV, which does something similar with CSV files (granted yours would probably be parseable as is with the appropriate sep_char setting on the Text::CSV_XS instance).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://618456]
Approved by clinton
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-05-16 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (62 votes). Check out past polls.

    Notices?