Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to extract the filenames in an array

by Anonymous Monk
on Sep 17, 2008 at 04:13 UTC ( [id://711862]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am a beginner in perl scripting. How do i can extract the file name in an array so that i can use some part of the file name?
I have tried something like this.
#!/usr/bin/perl -w
@namearr = `ls /usr/C`;
@name1=$namearr[0];
print ("$name1[2]\n");
Thanks.
  • Comment on How to extract the filenames in an array

Replies are listed 'Best First'.
Re: How to extract the filenames in an array
by GrandFather (Saint) on Sep 17, 2008 at 04:27 UTC

    Most likely you want to use something like File::Basename to manipulate file names and paths.

    Nice to see you are using warnings. You will find that strict (use strict;) helps a lot too.


    Perl reduces RSI - it saves typing
Re: How to extract the filenames in an array
by lamp (Chaplain) on Sep 17, 2008 at 05:24 UTC
    If you want to find all the filename of a particular directory, you can use 'opendir' and 'readdir' function. For eg.
    opendir(DIR, "/tmp") || die "can't opendir $!"; while (my $file = readdir(DIR)) { next unless (-f "/tmp/$file"); print $file."\n"; } closedir DIR;
    If you want check a particular file name, you can use the '-e' option. For eg.
    my $fname = "/tmp/test.txt"; print $fname if(-e $fname);
      The -e function checks for existence of a given path (among other functions which check for other things, of course).
Re: How to extract the filenames in an array
by CountZero (Bishop) on Sep 17, 2008 at 06:08 UTC
    use strict; my $dirpath = '/usr/C'; opendir(my $dir, $dirpath) or die "Error when using opendir $!"; my @namearr = grep {-f "$dirpath/$_"} readdir($dir); # get only files +and skip directories

    It is usually more efficient to use Perl's own internal functions rather than shelling out to an external command or program.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to extract the filenames in an array
by jwkrahn (Abbot) on Sep 17, 2008 at 05:08 UTC

     @name1=$namearr[0]; is the same as saying  $name1[0]=$namearr[0]; so there is nothing in  $name1[2] to print out.

Re: How to extract the filenames in an array
by repellent (Priest) on Sep 17, 2008 at 06:44 UTC
    Welcome to PerlMonks!

    Let's say we have the path: /login/user/temp/file.txt

    Imagine splitting the path names into 5 containers. This is how we would do it:
    #!/usr/bin/perl -l use warnings; use strict; use File::Spec::Functions qw(splitdir); my $path = '/login/user/temp/file.txt'; my @names = splitdir $path; print $names[0]; # prints: "" print $names[1]; # prints: "login" print $names[2]; # prints: "user" print $names[3]; # prints: "temp" print $names[4]; # prints: "file.txt"

    The first array element is an empty string because nothing comes before the first directory separator of $path.
Re: How to extract the filenames in an array
by Grey Fox (Chaplain) on Sep 17, 2008 at 14:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://711862]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found