Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Put "ls" in array

by serf (Chaplain)
on Apr 26, 2012 at 09:57 UTC ( #967288=note: print w/replies, xml ) Need Help??


in reply to Put "ls" in array

The answer really depends on whether you're trying to learn about populating arrays and are using the output of ls as an example, or trying to list files in a directory and get those into your program.

If the main aim is to get the list of files into your program, I'd recommend pelagic's answer. The opendir documentation has boilerplate code that you can copy into your program and modify to suit your needs. It's normally better in terms of efficiency, powerfulness and control over what's going on to do things like this internally in Perl (which is what opendir does) where you can, rather than running an external shell command to get the same data and then parsing its text output.

If you're learning about arrays, then the backticks & the glob work, as would using an open which allows you to process the output of the command on the way into your array.

e.g.:
#!/usr/bin/perl # # # use warnings; use strict; my @files; open (my $ls_fh, "/bin/ls|") || die "Can't run '/bin/ls': $!\n"; while (defined (my $line = <$ls_fh>)) { # next if $line =~ /pattern_I_want_to_ignore/; chomp $line; push @files, $line; } close $ls_fh; print "Third file: $files[2]\n"; for my $file ( @files ) { print "$file\n"; }

Replies are listed 'Best First'.
Re^2: Put "ls" in array
by JavaFan (Canon) on Apr 26, 2012 at 10:09 UTC
    What a bunch of lines! If I wanted to ignore some pattern, why not:
    my @files = grep {!/pattern_I_want_to_ignore/} `ls`;
    Or for the grasshoppers who haven't realized yet that Perl started its life as a glue language, and never deviated from that, and hence get the rashes as soon as they see a call to an external program:
    use autodie; opendir my $dir, "."; my @files = grep {!/pattern_I_want_to_ignore/} sort grep {!/^\./} # simulate ls readdir $dir;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967288]
help
Chatterbox?
[Lady_Aleena]: james28909, you might want to look at the other two on my scratchpad.
[james28909]: after you send to a sub, you can check if it is array or ref ect with ref
[james28909]: check if it is a hash or an array ect with ref in one sub.
[james28909]: like all the subs calling get_data get_array get_ect, you could just use get_data. once you send the data to get_data, check if it is a hash or an array ect. and do functions for each, in one sub
[james28909]: that looks like it would be alot more work than just renaming the sub though lol
[Lady_Aleena]: james28909, did you even look at the subs?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-05-24 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?