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!
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!