Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: how to Check file type?

by ChuckularOne (Parson)
on Dec 02, 2010 at 19:07 UTC ( #874990=note: print w/replies, xml ) Need Help??


in reply to how to Check file type?

Another way to grab the extension is to put the split name in an array and pop the last element.
#!/usr/bin/perl -w my $filename = "this.is.my.filename.txt"; my @bits = split /\./, $filename; print pop @bits;

Replies are listed 'Best First'.
Re^2: how to Check file type?
by TomDLux (Vicar) on Dec 02, 2010 at 19:44 UTC

    Saving the whole array and then popping off the last element uses up lines and adds variables. I'd rather wrap the split in parentheses and reference the last element.

    my $suffix = ( split /\./, $filename )[-1];

    The only danger is that if the only reason you're getting the suffix is to print it, you need more parentheses to prevent print from thinking a subset of the expression is its argument list:

    print ( split /\./, $filename )[-1]; # wrong print (( split /\./, $filename )[-1]); # unpleasant

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Thanks, that was basically (however inelegantly) what I was trying to accomplish. Printing was just for demonstration purposes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://874990]
help
Chatterbox?
choroba fears Amsterdam might fill him with enthusiasm to do nothing
[robby_dobby]: Uff, too expensive :-(
[LanX]: ... I found YAPC EU more entertaining (and international )than YAPC NA
[robby_dobby]: What's the site for YAPC::EU?
[LanX]: Robby Dobby no IIRC Sinai is already part of Asia
[robby_dobby]: blogs.perl.org page for yapceu is out of date by 2 years
[robby_dobby]: LanX: Sharm-el-Sheikh is in Egypt
[choroba]: act.yapc.eu
[robby_dobby]: Thanks, choroba
[LanX]: yes and Egypt (like Turkey and Russia) belongs to two continents

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-04-24 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (442 votes). Check out past polls.