Say you have a file with contents:
AABB 7323|Jan 28, 2003|Random Data Here
YZZ 2852|Mar 3, 2007|Data Data
YZZ 2559|Aug 2, 2010|More Data
AABB 2383|Jun 1, 2011|Data Field Data
The following perl script can list the unique key types:
#!/usr/bin/perl
use strict;
use warnings;
my $filename = "testinput.txt";
open (ROWS, "<$filename");
my $delimiter = '\|';
my %values = map {split / /, $_ } keys %{{map { split $delimiter, $_, 2 } <ROWS>}};
my @uniques = keys %values;
print join ("\n", @uniques);
exit 0;
which outputs:
AABB
YZZ
Note: when assign an array to a hash lvalue, the array elements are assign sequentially in key value pairs, since there's no such thing as hash context. see http://tutorials.freeskills.com/professional-perl-part-3-prototypes.htm
Replace nested-if's with proper exception-handling
-
" Why handle exceptions separately:
■ Handling exceptions separately enables you to define the main logic of
your
code together.
■ Without the use of sepa...
2 years ago
No comments:
Post a Comment