[Purdue-pm] Reminder

Phillip San Miguel pmiguel at purdue.edu
Tue Jan 23 09:42:39 PST 2007


David Jacoby wrote:
> Tuesday night is Social Meeting time! 7pm at Cafe Royal on
> fabulous Chauncey Hill. See you there!
>
>   
I'll be there.
By the way. I should probably present this at the next technical 
meeting, but I'll probably have forgotten it by now. Kewl way to pack a 
white space delimited list with a single header line containing column 
names:

=pod
For example, data looks like:
Row Column  spot   call
A   1   1   +
A   1   2   +
A   1   3   -
A   1   4   +
A   1   5   +
A   1   6   -
=cut

#read first (header) line and parse column names
my @col_names;
while (<>){
    #This allows the first row to set the field order
    @col_names   = split;
    last;
}
my @array_of_hash_records
#Now read all the rest of the lines
while (<>){
    my %record;     #hash to parse fields
    #assign to hash slice. Column headers are keys.
    @record{@col_names} = split;    #kewl! column names become hash keys
    push @array_of_hash_records, \%record   #save each record to data 
structure
}


Weird things here are the "hash slice". Which I think means:

@hash{'a','b','d'}

is the same as

($hash{'a'},$hash{'b'},$hash{'c'})

and, instead of writing out the keys, you can just use an array:

@array=('a','b','d'); @hash{@array}

is the same as

@hash{'a','b','d'}


Phillip


More information about the Purdue-pm mailing list