What is the best way to do this? I need to assign values from a split, but I'd like to throw away values that are meaningless or unwanted.

I can think of a few other ways to do this, but these were the best two I came up with.
#!/usr/bin/perl -w use strict; # Use undef hash values for unneeded fields my @fields = ( 'FIRSTNAME', 'LASTNAME', 'PHONE', undef, # Useless data undef, # Useless data undef, # Useless data 'FAX', undef, # Useless data 'CITY', 'STATE', ); # Use dummy key for unneeded fields my @fields2 = ( 'FIRSTNAME', 'LASTNAME', 'PHONE', 'DUMMY', # Useless data 'DUMMY', # Useless data 'DUMMY', # Useless data 'FAX', 'DUMMY', # Useless data 'CITY', 'STATE', ); while (<DATA>) { chomp; my (%record, %record2); # Using undef keys; requires disabling warnings # about uninitialized values in hash slice no warnings; @record{@fields} = split(/\t/, $_); print "1) First Name: $record{FIRSTNAME}, State: $record{STATE}\n" +; # Using dummy key, then deleting it use warnings; @record2{@fields2} = split(/\t/, $_); delete $record2{'DUMMY'}; print "2) First Name: $record2{FIRSTNAME}, State: $record2{STATE}\ +n"; } print "\n---------------\nDone.\n"; exit; __DATA__ Bill Davis 999-888-7777 KH101 1 1 999-888-7770 19 +65 Chantilly VA Bob Rogers 999-777-8888 KH101 1 1 999-777-8880 19 +53 Dallas TX Jim Dawson 999-787-8787 KH101 1 1 999-787-8787 19 +72 San Diego CA Harry Jones 999-878-7878 KH101 1 1 999-878-7870 1 +963 Chicago IL John Black 999-778-7788 KH101 1 1 999-778-7788 19 +36 Topeka KS
Which of these is the better solution, or does someone (I hope) have another solution that would be best?

Impossible Robot

In reply to 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.