Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967284]
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-07-26 01:05 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (175 votes), past polls