impossiblerobot has asked for the wisdom of the Perl Monks concerning the following question:
Which of these is the better solution, or does someone (I hope) have another solution that would be best?#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ignoring values in a hash slice assignment
by grep (Monsignor) on Feb 28, 2002 at 21:54 UTC | |
by impossiblerobot (Deacon) on Feb 28, 2002 at 22:06 UTC | |
by grep (Monsignor) on Feb 28, 2002 at 22:17 UTC | |
|
Re: Ignoring values in a hash slice assignment
by dragonchild (Archbishop) on Feb 28, 2002 at 21:55 UTC | |
|
Re: Ignoring values in a hash slice assignment
by petral (Curate) on Feb 28, 2002 at 22:05 UTC | |
|
Re: Ignoring values in a hash slice assignment
by seattlejohn (Deacon) on Mar 01, 2002 at 08:01 UTC | |
|
Re: Ignoring values in a hash slice assignment
by impossiblerobot (Deacon) on Mar 01, 2002 at 16:53 UTC | |
by dragonchild (Archbishop) on Mar 01, 2002 at 17:45 UTC |