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

Files in a directory

by amma (Novice)
on Apr 28, 2013 at 10:34 UTC ( #1031049=perlquestion: print w/ replies, xml ) Need Help??
amma has asked for the wisdom of the Perl Monks concerning the following question:

p_abc_xyz_123.txt

p_abc_xyz.txt

p_abc.txt

are the files I require among many files

I will be given only one file name p_abc_xyz_123.txt. I have to find the remaining above files. open each file for writing and write few statements in files depending on pattern macthing

Any initial codes that would help me

Comment on Files in a directory
Re: Files in a directory
by choroba (Abbot) on Apr 28, 2013 at 10:42 UTC
    Use split:
    #!/usr/bin/perl use warnings; use strict; use feature qw(say); my $filename = 'p_abc_xyz_123.txt'; my ($basename, $suffix) = split /\./, $filename; my @parts = split /_/, $basename; for my $i (1 .. $#parts) { say join('_', @parts[0 .. $i]), ".$suffix"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Files in a directory
by hdb (Parson) on Apr 28, 2013 at 11:10 UTC

    I always prefer regexes, but have to admit that choroba's proposal is much cleaner.

    use strict; use warnings; my $filename = "p_abc_xyz_123.txt"; print "$1$2$3\n" while( $filename =~ s/(.*)(_\w{3})(\..*)/$1$3/ );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-08-28 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls