![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
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 |
| Need Help w/PERL system command | lorik | Shell Programming and Scripting | 1 | 12-06-2006 08:09 PM |
| Perl system ssh find question | thumper | Shell Programming and Scripting | 3 | 07-19-2006 04:41 PM |
| Perl - backticks v system in if statements | gjkeenan | Shell Programming and Scripting | 5 | 07-31-2005 06:41 AM |
| Which version of perl is installed on my system? | kfad | Shell Programming and Scripting | 6 | 04-20-2005 09:44 PM |
| Perl run system command | gdboling | Shell Programming and Scripting | 1 | 09-02-2003 10:30 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
perl - system command
Can this be done without using te system command? I have a directory with a large number of files in it, but I am interested in only the 8 most recent.
The directory looks like -rw-rw-rw- 1 adsm adsm 13412 Sep 22 08:31 events_dump_09222005.csv.gz -rw-rw-rw- 1 adsm adsm 13405 Sep 21 08:31 events_dump_09212005.csv.gz -rw-rw-rw- 1 adsm adsm 13237 Sep 20 08:31 I am getting the date out of the file to be used in another module. What I have works, but want I want to know is if it can be done without resorting to temporary file (listfiles) and the system command #!/usr/bin/perl system( 'ls -tr /adsm/DATASRC/events/ | tail -8r > /adsm/SCRIPTS/BKUPINFO/listfiles'); open (LSTFILES, "</adsm/SCRIPTS/BKUPINFO/listfiles"); while (<LSTFILES>) { print($_); ($pt1,$pt2) = split(/_dump_/, $_ ); chomp($pt2); print "$pt2\n"; ($pt3,$pt4) = split(/\./,$pt2); print "$pt3\n"; } close(LSTFILES); |
|
||||
|
Yes, in fact I won't resort to shell equivalents if something can be solely done from within Perl, because this ensures the highest portability. The following will give you a list of top 8 files with the latest mtime in descending order of mtime:
Code:
#!/usr/bin/perl
my $base = "/adsm/DATASRC/events/";
opendir DIR, $base or die "Cannot read dir!";
my @list = readdir DIR;
closedir DIR;
my @list_with_mtime = map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]]:()) } @list;
my @r_sorted_list = map { $$_[0] } sort { $$b[1] <=> $$a[1] } @list_with_mtime;
# Show top 8
{
local $, = "\n";
print @r_sorted_list[0..7];
}
|
|
||||
|
Thanks for the reply. It absolutely does what I am looking for.
Now I need to understand it. My understanding of the map function is that it works like a foreach loop. Is that right? Can you explain this statement map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]] )) } @list |
|
||||
|
It looks complex, but it is actually quite simple if I put it in a nicer looking form.
Code:
map {
(
$_ !~ /^\./?
[
$_,
(stat($base . $_))[9]
]:
()
)
} @list
It will pass through every entry of @list and collect the evaluated value in the map{} block and return the aggregated list. At the end of this statement we will get an array of array references containing the filename and the corresponding mtime. Entries that yield an empty array are ignored because an empty array upon interpolated in a list will "dissolve" itself. The net effect in this piece of code is that hidden files will disappear from the resulting, aggregated list. This is what we want if we want to ignore all hidden files. i.e. ("a", "b", (), "c") will give ("a", "b", "c") so this is how this trick works. Yes, the next statement sorts this intermediate list in descending order of mtime, and then reconstruct an array with filenames only (by cleaning the temporary mtime values and removing the references). |
|
||||
|
As I said, I have made some temporary references with the first statement. In my case this is an array reference (think about it like a pointer to an array) containing the filename together with the mtime as two separate array items.
To access an item from a normal array by index you will $array[0], $array[1] If the corresponding array is pointed to by a reference and you need to access an item through the reference, that becomes $$arrayref[0], $$arrayref[1] OR (both syntax will work) $arrayref->[0], $arrayref->[1] which literally means "dereference and then index in the underlying array". Understanding these require some knowledge about references in Perl. The official page that talks about this: http://perldoc.perl.org/perlref.html |
![]() |
| Bookmarks |
| Tags |
| mtime |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|