Perl - New line in array elements

Hello,
I have a comma delimited input feed file. The first field has directory location and the second field has file name.
Ex of input feed:

/export/appl/a,abc*.dat
/export/appl/b,xyz*.dat
/export/appl/c,pmn*.dat

Under each directory, there would be many files like...
.
.
.
abc.200801.dat
abc.200802.dat
abc.200803.dat
abc.200804.dat

I'm writing a perl script, in which part of the logic is to get the stat() of the latest file in the corresponding directory. (in this case I need to get stat() of abc.200804.dat

So, first I'm getting the directory and then chdir to that location and doing a ls -ltr with the wild card file names and storing them in array. Then using $

foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=$list_files[-1];    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

The problem is, I think when I'm trying to get the last changed file into the array element, its storing the latest file in the last element, but I think its appending new line to the element along with file name. So, the stat($f_nme) isn't working.

Can you please let me know what I'm missing?
Thanks!

Evaluating backticks in list context, Perl does not automatically drop the newlines from the command output for you. You should do that yourself.

Compare the following two cases:

[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = `ls /usr/local`; print Dumper(\@lines)'
$VAR1 = [
          'Adobe
',
          'apps
',
          'bin
',
          'Brother
',
          'etc
',
          'games
',
          'include
',
          'lib
',
          'libexec
',
          'man
',
          'RealPlayer
',
          'sbin
',
          'share
',
          'src
'
        ];
[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = map { chomp; $_ } (`ls /usr/local`); print Dumper(\@lines)'
$VAR1 = [
          'Adobe',
          'apps',
          'bin',
          'Brother',
          'etc',
          'games',
          'include',
          'lib',
          'libexec',
          'man',
          'RealPlayer',
          'sbin',
          'share',
          'src'
        ];

As you can see, there are trailing newline with the first case, but not after post-processing each item removing the trailing newlines.

Basically what he said... Use chomp($f_nme[-1]) first, then assign it. Use chomp() above where you get the directory list from your input file too... Is safer. chop() will strip the last char of a string ALWAYS. chomp() strips only if is a LINE ENDING, so is much safer usually if you are not always SURE that the last char of your string is the line ending you want to remove....

I should have mentioned it.... I've used both chomp($list_files[-1]) and chop($list_files[-1]). It doesn't seem to work.

my $f_nme=chomp($list_files[-1]); 
print " last file name is $f_nme \n";

It doesn't assign any value to $f_nme.

And chomp(($list_files[-1]) returns "1" ?!

foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=chomp($list_files[-1]);    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

Anyways, I have used the way cbhikong suggested and it worked. Thanks!

Yes, you misunderstand chomp() chop(). They actually transform their argument. What they RETURN is a 1 or 0 (I think)...

So $y = chop($x); actually changes $x and assigns 1 (success) to $y. So you don't ASSIGN the results, you just do it....

$x = "abcd";

chop($x);

Now $x is "abc"