Reading part of a file name
Lhamo_rin
created: 2006-04-05 07:15:50
I have a string that contains a file name. I want to remove the prefix. How can I do this? For example $string = FileName.089-09_08_45 I just want to reduce this to 08-09_08_45.
Re: Reading part of a file name
created: 2006-04-05 07:20:47

[Lhamo_rin], i dont know what you are really going to achieve, but as per your input and output, i assume, you can accomplish using regex as shown below.

$string = 'FileName.089-09_08_45';
$string =~ s/FileName\.(\d{2})\d/$1/;
print $string;

output:
08-09_08_45

Prasad

Re: Reading part of a file name
created: 2006-04-05 07:21:43
Hi, Just try this
$string = 'FileName.089-09_08_45';
$string =~s#([a-z\.]+)([\d\-_]+)#$2#gsi;
print $string;
Regards
Perlsen
Re: Reading part of a file name
created: 2006-04-05 07:25:17

Hi, Give some more information then only we will help you.

Anyway try this, if i understood your question correctly.

use strict;
use warnings;

my $string = "FileName.089-09_08_45";

$string =~ s/^[^\d]+//g; ##if Filename doesn't contain digits
#or
$string =~ s/^[^\.]+\.//g; ##if Filename doesn't contain dots

print $string;
__END__
089-09_08_45

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Reading part of a file name
created: 2006-04-05 07:30:26
My money is on "FileName.089-09_08_45" being a typo which should have read "FileName.08-09_08_45", in which case

$string =~ s/^FileName\.//;

would do the trick.

Cheers,

JohnGG

Re: Reading part of a file name
created: 2006-04-05 07:30:44

If your file names are consistently like your example, use split :

use strict;
use warnings;

opendir( DIR, "/home/Lhamo/files" ) or die "painfully: $!";
my @files = readdir DIR;
closedir DIR;

for ( @files ) {
    next if ( $_ eq '..' );
    next if ( $_ eq '.' );
    my ( $filename, $code_number ) = split( /./, $_, 2 );
    print "$code_number\n";
}

I made a mistake. I don't know where, but I always do. I haven't tested this code. Someone will point it out shortly :) In any case, you get the idea.

--
-- GhodMode
Blessed is he who has found his work; let him ask no other blessedness.
-- Thomas Carlyle
Re^2: Reading part of a file name
created: 2006-04-05 09:03:16
You have use the pattern /./ on which to split the string. '.' is a regular expression metacharacter which matches any single character (except the newline in multi-line matching) so your split isn't doing what you want. You should have escaped the metacharacter like this

my ($filename, $code_number) = split(/\./, $_, 2);

so that you are splitting on a literal full-stop.

Also, your

    next if ( $_ eq '..' );
    next if ( $_ eq '.' );

can be more neatly achieved by

    next if /^\.\.?$/;

Cheers,

JohnGG

perlmonks.org content © perlmonks.org and GhodMode, johngg, Lhamo_rin, perlsen, prasadbabu, Samy_rio

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03