Similair command like 'ls' in perl

  • Thread starter Petterson Mikael
  • Start date
P

Petterson Mikael

Hi,

When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl. Why I
need this? I need to find out which directory ( in a directory) that was
created last.

BR

//Mikael
 
J

Josef Moellers

Petterson said:
Hi,

When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl. Why I
need this? I need to find out which directory ( in a directory) that was
created last.

perldoc -f stat
 
G

Glenn Jackman

At 2005-01-05 07:54AM said:
Hi,

When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl. Why I
need this? I need to find out which directory ( in a directory) that was
created last.

opendir my $d, $directory;
my @dirs = map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map {[$_, -M]}
grep {-d}
readdir $d;
closedir $d
# I assume by "created last" you mean newest
my $last = "$directory/$dirs[0]";
 
P

Paul Lalli

Petterson Mikael said:
Hi,

When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl. Why I
need this? I need to find out which directory ( in a directory) that was
created last.

This is what we call an "XY Problem". Fortunately, it's one that you
corrected in the same message. Basically, you want to know X, the
creation time of a certain directory. But you've already decided that
the best way to do this is Y - to obtain a directory listing ala `ls`.
So instead of asking us how to do X, you've asked us how to do Y.

To solve you're actual problem, X, checkout
perldoc -f -X

(in this case, the -X above is coincidental to the XY problem)

Paul Lalli
 
R

Robert Sedlacek

Petterson said:
Any hints?

Hints? Many. Perl has a very good documentation, just take a look at
'perldoc perlfunc'[0]. Sorry, I'm not a friend of giving answers in
'out of the box'-style.

g,
Robert

[0] Should work in google as well as in your cli.
 
P

Paul Lalli

Glenn Jackman said:
opendir my $d, $directory;
my @dirs = map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map {[$_, -M]}
grep {-d}
readdir $d;

Please see the warning in perldoc -f readdir to learn why this won't
work.

Paul Lalli
 
S

Sherm Pendley

Petterson said:
When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl.

Have a look at:

perldoc -f opendir
perldoc -f readdir
perldoc -f stat

sherm--
 
J

Joe Smith

Petterson said:
When I do 'ls' in a shell I get a whole bunch of information including a
timestamp. Is it possible to get the same information in perl.

Check the docs:
perldoc -f -X

For an example of using stat(), take a look at the output of
find2perl . -ls

For an example of how -M() can be used, try this one-liner:
perl -e '%c=map {-M $_,$_} <* .*>; printf "%8.2f %s\n",$_,$c{$_} for
sort {$a<=>$b} keys %c'
 
G

Glenn Jackman

At 2005-01-05 03:03PM said:
perl -e '%c=map {-M $_,$_} <* .*>; printf "%8.2f %s\n",$_,$c{$_} for sort {$a<=>$b} keys %c'

That may not work as you expect.
Filenames are unique in a directory, but last modified times aren't.
Your %c hash may be missing files.
 
J

Joe Smith

Glenn said:
That may not work as you expect.
Filenames are unique in a directory, but last modified times aren't.
Your %c hash may be missing files.

Yes, I know. A proper solution would be to use something like a
Schwartzian Transform.
-Joe
 
A

Anno Siegel

Joe Smith said:
Yes, I know. A proper solution would be to use something like a
Schwartzian Transform.

You can use a hash in "something like" a Schwartzian Transform, just
the other way around:

my %c = map { $_ => -M $_ } <* .*>;
printf "%8.2f %s\n", $c{ $_}, $_ for
sort { $c{ $a} <=> $c{ $b} } keys %c;

Like the Schwartzian Transform, it calculates the sort key only once for
each list element. In this case it wouldn't have any advantages over
the standard Schwartzian (in fact it would be a little slower since
hashes are slower than arrays).

When there are duplicates in the list to be sorted, it *can* have an
advantage when the hash is built like this:

my %c;
@c{ @array} = ();
$c{ $_} = sort_key( $_) for keys $c;
# etc...

This way, the sort key is only calculated once for every unique element
in the list.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top