![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl -write values in a file to @array in perl | meghana | Shell Programming and Scripting | 27 | 06-07-2009 06:05 PM |
| pass each elements in array to ssytem command | jaganadh | Shell Programming and Scripting | 0 | 12-14-2007 06:57 AM |
| To return the elements of array | Sudhakar333 | Shell Programming and Scripting | 5 | 08-06-2007 03:20 PM |
| How can i read array elements dynamically in bash? | haisubbu | UNIX for Dummies Questions & Answers | 1 | 08-29-2006 03:19 AM |
| how to return an array of elements from oracle to shell script | satyakiran | Shell Programming and Scripting | 3 | 08-02-2005 10:57 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 $ Code:
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: Code:
[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....
|
|
||||
|
Quote:
Code:
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" ?! Code:
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! Last edited by bperl; 04-02-2008 at 11:47 PM.. |
|
||||
|
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" |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|