#!/usr/bin/perl
use strict;
use warnings;
my $directory = shift @ARGV or die "Please specify a directory";
if(opendir(DIR , $directory))
{
my @files = readdir(DIR);
@files = grep(/\.out/ , @files);
closedir(DIR);
foreach my $file (@files)
{
if ( -z $directory.$file )
{
next;
}
ProcessData($directory.$file);
}
}
else
{
print STDERR "unable to open current directory\n";
}
sub ProcessData
{
my ($file) = @_;
if(open(FILE , $file))
{
my @lines = <FILE>;
my $lines =~ s/\,//g;
my @fields = split(/\s+/,$lines[1]);
print "$fields[1] $fields[2] $fields[3] $fields[4] $fields[5]\n";
close FILE;
}
else
{
print STDERR "Can't open $file\n";
}
I'm trying to split the second line of a set of files on white space, remove all commas, and then print some of the fields. The correct fields are printed but the comma remains, and I am given the error message: Use of uninitialized value $lines in substitution (s///)
. I am fairly new to Perl and quite confused by this. Any help would be hugely appreciated. Thanks in advance.