Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Getting a base name

by Anonymous Monk
on Jul 20, 2005 at 08:20 UTC ( #476409=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Master,
Why this code:
perl -e ' $fname = 'test.txt'; $base = (split(/./,$fname))[0]; print "$base\n";'
Doesn't prints me:
test
Thanks a million again.

Comment on Getting a base name
Select or Download Code
Re: Getting a base name
by Smylers (Pilgrim) on Jul 20, 2005 at 08:30 UTC

    Put a backslash before the dot.

    Smylers

Re: Getting a base name
by Iron (Scribe) on Jul 20, 2005 at 08:30 UTC
    You should use

    $base = (split(/\./,$fname))[0];

    and better solution is File::Basename
Re: Getting a base name
by Tanalis (Curate) on Jul 20, 2005 at 08:31 UTC
    You need to escape the '.' in your split:
    $base = ( split(/\./,$fname) )[0];
    Adding -Mstrict and -w (always a good idea), the following works fine for me:
    perl -Mstrict -w -e ' my $fname = "test.txt"; my $base = ( split(/\./,$fname) )[0]; print "$base\n";'
    Update: And, as others have mentioned, you have single quotes inside single quotes, too :)

    Hope that helps ..

Re: Getting a base name
by reasonablekeith (Deacon) on Jul 20, 2005 at 08:32 UTC
    Your dot is matching anything, you wanted to match a dot explicitly
    $base = (split(/\./,$fname))[0];
    UPDATE: Too slow, that's probably enough replies now :-)
    ---
    my name's not Keith, and I'm not reasonable.
Re: Getting a base name
by polettix (Vicar) on Jul 20, 2005 at 08:33 UTC
    You're using single quotes inside single quotes; moreover, the dot is a special character in a regexp, you have to escape it:
    perl -e ' $fname = "test.txt"; # Double quotes! $base = (split(/\./,$fname))[0]; # Escape dot! print "$base\n";'

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Getting a base name
by gube (Parson) on Jul 20, 2005 at 08:33 UTC
    $fname = 'test.txt'; $base = (split/\./, $fname)[0]; print "$base\n";
Re: Getting a base name
by SimonClinch (Chaplain) on Jul 20, 2005 at 08:40 UTC
    The delimiter for split is being parsed locally as a regular expression wildcard which is not very useful in this context. Either escaping it with backslash /\./ or enclosing in single quotes '.' will do the trick, although the latter will be more difficult in this case given that the whole program is also enclosed in single quotes.

    One world, one people

Re: Getting a base name
by Zaxo (Archbishop) on Jul 20, 2005 at 08:49 UTC

    Iron wisely suggests File::Basename. Here's how it goes,

    use File::Basename; my $filename = "../../path/to/test.txt"; my $basename = basename( $filename, '.txt', '.dat'); print $basename, $/; __END__ test
    If you omit the extension list arguments, or if $filename's extension is not in the list, basename() will return the basename with extension.

    F::B also offers the functions dirname(), which extracts just the directory part, and fileparse(), which returns a list of ($basename, $dirname, $extension)

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-25 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (162 votes), past polls