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

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

Replies are listed 'Best First'.
Re: Files in a directory
by choroba (Bishop) 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 (Monsignor) 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
[marto]: you have a balcony on the Sun?
marto ducks
[Discipulus]: bravo! orange juice will help
[marto]: not on the Sun, it'd vaporise

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-03-21 11:12 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (267 votes). Check out past polls.