#!/usr/bin/perl
use strict;
use warnings;
my $line = "ssn|empNo|ncEmpName|empName|hireDate|ncAddr|addr|state|ncCity|city|zip";
my @columns = split /\|/, $line;
print "@columns[-1,-4,4,3]\n"; # "zip state hireDate empName"
####
#!/usr/bin/perl
use strict;
use warnings;
#empNo|ncEmpName|empName|hireDate|ncAddr|addr|state|ncCity|city|zip";
my $line2 = "123445678 45612 11 Steve Smith 11012015 16 1001 Main Street GA 7 Atlanta 30553 x y z";
# Note: Looks like ncEmpName is "45612 11", a fixed width field
my @format_spec = qw(
v empNo
f8 ncEmpName
f11 enpName
v hireDate
v ncAddr
f16 addr
v state
v ncCity
f7 city
v zip
v x
v y
v z
);
my $regex = "^";
while (@format_spec)
{
my $format = shift @format_spec; # pair wise in List::Util possible
my $name = shift @format_spec; # here keep it simple
if ($format =~ /v/) #variable length (no embedded spaces)
{
$regex .= '\s*(\S+)';
}
elsif ( (my $width) = $format =~ /\s*f(\d+)/) # fixed length,means
# embedded spaces
{
$regex .= '\s*(.{' . "$width})"; # \s cannot be within ""
}
print "$regex\n"; #for debug, comment this out later
}
my (@tokens) = $line2 =~ /$regex/;
print join ("|", @tokens), "\n";
__END__
The regex is built like this:
^\s*(\S+)
^\s*(\S+)\s*(.{8})
^\s*(\S+)\s*(.{8})\s*(.{11})
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)\s*(.{7})
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)\s*(.{7})\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)\s*(.{7})\s*(\S+)\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)\s*(.{7})\s*(\S+)\s*(\S+)\s*(\S+)
^\s*(\S+)\s*(.{8})\s*(.{11})\s*(\S+)\s*(\S+)\s*(.{16})\s*(\S+)\s*(\S+)\s*(.{7})\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)
The "|" separated line is like this:
123445678|45612 11|Steve Smith|11012015|16|1001 Main Street|GA|7|Atlanta|30553|x|y|z
##
##
@tokens = map{s/\s*$//; $_;}@tokens; #delete trailing spaces