Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Reading part of a file name

by GhodMode (Pilgrim)
on Apr 05, 2006 at 11:30 UTC ( [id://541350]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found