Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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:




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
Replies are listed 'Best First'.
Re: Files in a directory
by choroba (Canon) 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 (Prior) 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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-11-25 20:29 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (690 votes), past polls