Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Put "ls" in array

by jethro (Monsignor)
on Apr 26, 2012 at 09:36 UTC ( #967284=note: print w/ replies, xml ) Need Help??


in reply to Put "ls" in array

You can get the output of system commands like ls into an array with this:

my @array= `ls`;

Note that these are backticks, not the normal quotes. Also you might use chomp() afterwards on the array to remove the line endings

In your case it would also be possible to use opendir()/readdir() to read a directory like a file

The order of the files on disk will look pretty random to you by the way (at least if you have deleted and created lots of files in this directory already, otherwise they might be in order of creation). If you want to sort them, perl has a sort() function as well


Comment on Re: Put "ls" in array
Download Code
Re^2: Put "ls" in array
by bimleshsharma (Beadle) on Apr 27, 2012 at 09:24 UTC

    It's totally depends on your requirment. If you simply want to push into array and list then here is snippet..

    my @dir= `ls`; print "\n @dir"; output: #test# allcases bin codebase core execute_dir nonqa precutoff reversal stamp1 stamp2 start_epicDB.pl test.pl test.sh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967284]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-12-21 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls