in reply to Reading part of a file name

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

Replies are listed 'Best First'.
Re^2: Reading part of a file name
by johngg (Canon) on Apr 05, 2006 at 13:03 UTC
    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