Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do i take the first 4 characters of any file name in a specified directory?

by Anonymous Monk
on Sep 17, 2008 at 06:02 UTC ( #711881=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi
How do i take the first 4 characters of any file name in a specified directory?
I have tried this.
#!/usr/bin/perl -w
@namearr = `ls -l /usr/C`;
foreach $nameline (@namearr)
{
print ("$nameline\n");
@name1 = $nameline;
print ("$name1 [3]");
}

where C is a directory.
Thanks

Comment on How do i take the first 4 characters of any file name in a specified directory?
Re: How do i take the first 4 characters of any file name in a specified directory?
by lamp (Chaplain) on Sep 17, 2008 at 06:26 UTC
    You can use 'printf' function for printing the first 4 characters of a file name. For eg.
    opendir(DIR, "/tmp") || die "can't opendir $!"; while (my $file = readdir(DIR)) { next unless (-f "/tmp/$file"); printf '%.4s', $file; } closedir DIR;
Re: How do i take the first 4 characters of any file name in a specified directory?
by dHarry (Abbot) on Sep 17, 2008 at 06:26 UTC

    How about the substr function?

    I would use something differrent to read the content of a directory: the opendir, readdir and closedir functions. Also the strict and warnings pragma's are missing.

Re: How do i take the first 4 characters of any file name in a specified directory?
by pjotrik (Friar) on Sep 17, 2008 at 07:52 UTC
    In the spirit of TMTOWTDI:
    $name =~ /^(.{0,4})/ && print "$1\n";
    In the matter of obtaining the filenames, I fully agree with dHarry, use readdir & co.
      Don't forget the /s modifier on your regex, because a file name could actually contain a newline character (at least on unix-like platforms that's allowed).
Re: How do i take the first 4 characters of any file name in a specified directory?
by cstrong (Beadle) on Sep 17, 2008 at 10:14 UTC
    As the number of columns provided in ls -l are constant, how about splitting out each element of the column in to a variable and then matching the first four characters of the filename?
    @namearr = `ls -l`; foreach $nameline (@namearr) { chomp $nameline; print ("'$nameline'\n"); ($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8, $name) = ( +split " ", $nameline); $name =~ s/^(\w{4}).+/$1/; print ("NAME '$name'\n"); }
    The regex part matches the first four characters and substitutes the match in to the $name variable
Re: How do i take the first 4 characters of any file name in a specified directory?
by jonadab (Parson) on Sep 17, 2008 at 12:09 UTC
    @name1 = $nameline;

    I am almost certain this line doesn't do what you think it does. There are other ways to solve the problem, but if what you want is to assign all the characters in a string to an array, you can use the split function with an empty regex.

    Also, the print on the next line only prints out one of the array elements (specifically, the number 3 element, i.e., the fourth). If you want several elements at once, use slice syntax.

    HTH.HAND.
    -- 
    We're working on a six-year set of freely redistributable Vacation Bible School materials.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-10-22 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls