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.


In reply to Re2: Ignoring values in a hash slice assignment by dragonchild
in thread Ignoring values in a hash slice assignment by impossiblerobot

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.