perl split by |

I have a text file and each line looks like below


each line has lot of information about employee and seperated by |
each line has almost like 49 separators.
but i want only 12, 29, 36 th value
so I wrote my perl script as below but it did not work
what is the mistake.


use 5.006;
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Cwd;
use DateTime;
use DBD::mysql;
use DBI;

my $helpme = 0;
my $man = 0;
my $inputFileName = 'C:\\temp\\test.txt';
my $outputFileName = 'C:\\temp\\output.txt';

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die "No input file name specified!" if !$inputFileName;
die "No output file name specified!" if !$outputFileName;

open(INFILE, "<", $inputFileName) || die "Could not open input file!";
open(OUTFILE, ">", $outputFileName) || die "Could not open/create output file!";

my @where;
while (<INFILE>) {
   my @fields = split /\s*|\s*/;
   print "$fields[29]\n";

close INFILE;
close OUTFILE;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

my @fields = split /\s*\|\s*/;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Use an array slice if you only want a subset of the fields.

my @fields = (split /\s*\|\s*/)[12,29,36];
print "$fields[1]\n";

Open in new window

If you were counting your fields beginning at 1 instead of 0, then we'd need to adjust that to this:
my @fields = (split /\s*\|\s*/)[11,28,35];
print "$fields[1]\n";

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.