Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

Replies are listed 'Best First'.
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-04-27 03:00 GMT
Find Nodes?
    Voting Booth?