in reply to Re: Ignoring values in a hash slice assignment
in thread Ignoring values in a hash slice assignment

Ok. You're just not thinking it through. You say that you have a list of some 70 fields, some of which may be needed and others not. Given how you presented your original question, you're storing this list of fieldnames in an array, putting 'DUMMY' for the fields you wish to ignore.

That, my friend, is a maintainer's nightmare.

  1. You don't ever say what the field is you're ignoring
  2. You don't say WHY you're ignoring that field
  3. It isn't easy to change which fields you're ignoring without researching into what fields exist
  4. It also isn't easy to change which order the fields exist in.

So, we need some way of abstracting out which fields are there. Then, we need a way of identifying which fields we actually want. Lastly, we need a way of making sure that this information (which will become quite large) doesn't impact our main program.

Enter "Modular Design". Let's create a module where we store some data about what fields we want. Let's also create a few functions that allow us to talk about various aspects (or attributes) of each field.

Now, you don't need a full-blown OO implementation of this. That would be too much work and too unwieldy to manage. However, you can do a very nice Exporter implementation of this. Something along the lines of:

# Untested, but should work use 5.6.0; use strict; use warnings; package FieldStorage; use Exporter; our @ISA = qw(Exporter); # Using @EXPORT instead of @EXPORT_OK because we want these definitely + # in the caller's namespace. If we don't, use @EXPORT_OK. our @EXPORT = qw( getFieldNames isRequired ); my %Fields = ( FOO => { Order => 0, Required => 1, }, BAR => { Order => 1, Required => 0, }, .... ); sub getFieldNames { return sort { $a->{Order} <=> $b->{Order} } keys %Fields; } sub getFieldIndices { return map { $Fields{$_}{Order} } @_ || getFieldNames; } sub isRequired { my $field = shift; return undef unless exists $Fields{$field}; return $Fields{$field}{Required}; } 1; __END__
You would then use this module as so:
use FieldStorage; # Some stuff here my @fieldNames = grep { isRequired($_) } getFieldNames; my @fieldIndices = getFieldIndices(@fieldNames); my %records; @records{@fieldNames} = (split(/\t/, $_))[@fieldIndices];

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.