Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Reading part of a file name

by Lhamo_rin (Friar)
on Apr 05, 2006 at 11:15 UTC ( [id://541342]=perlquestion: print w/replies, xml ) Need Help??

Lhamo_rin has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Reading part of a file name
by prasadbabu (Prior) on Apr 05, 2006 at 11:20 UTC

    Lhamo_rin, i don't 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.

    $filename = 'FileName'; #any filename $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
by perlsen (Chaplain) on Apr 05, 2006 at 11:21 UTC
    Hi, Just try this
    $string = 'FileName.089-09_08_45'; $string =~s#([a-z\.]+)(\d{2})\d#$2#gsi; #or $string =~s#.*?(\d{2})\d(\-[\d|_]+)#$1$2#gsi; print $string;
    Regards
    Perlsen
Re: Reading part of a file name
by Samy_rio (Vicar) on Apr 05, 2006 at 11:25 UTC

    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
by GhodMode (Pilgrim) on Apr 05, 2006 at 11:30 UTC

    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
      You have used 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

Re: Reading part of a file name
by johngg (Canon) on Apr 05, 2006 at 11:30 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found