Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Parsing Pathnames

by carlriz (Beadle)
on Apr 03, 2014 at 20:14 UTC ( #1081001=perlquestion: print w/replies, xml ) Need Help??
carlriz has asked for the wisdom of the Perl Monks concerning the following question:

I have been having trouble parsing out these pathnames. What I want to do is to extract the name of the last file in the pathname. What is the best way to do this?

my @file_name_array = ( "/proc/cpuinfo", "/proc/meminfo", "/etc/fstab", "/proc/net/pf_ring/info", "/var/log/boot.log", "/etc/sysconfig/network-scripts/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth2", "/etc/sysconfig/network-scripts/ifcfg-eth3", "/etc/sysconfig/network-scripts/ifcfg-eth4", "/etc/udev/rules.d/70-persistent-net.rules" );

Replies are listed 'Best First'.
Re: Parsing Pathnames
by toolic (Bishop) on Apr 03, 2014 at 20:24 UTC
    File::Basename is one way...
    use warnings; use strict; use File::Basename qw(basename); my @file_name_array = ( "/proc/cpuinfo", "/proc/meminfo", "/etc/fstab", "/proc/net/pf_ring/info", "/var/log/boot.log", "/etc/sysconfig/network-scripts/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth2", "/etc/sysconfig/network-scripts/ifcfg-eth3", "/etc/sysconfig/network-scripts/ifcfg-eth4", "/etc/udev/rules.d/70-persistent-net.rules" ); print basename($_), "\n" for @file_name_array; __END__ cpuinfo meminfo fstab info boot.log ifcfg-eth0 ifcfg-eth1 ifcfg-eth2 ifcfg-eth3 ifcfg-eth4
Re: Parsing Pathnames ( Path::Tiny )
by Anonymous Monk on Apr 03, 2014 at 23:04 UTC
    use Path::Tiny qw/ path /; print path( qw[ /ro/sham/bo ] )->basename; __END__ bo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081001]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-12-18 03:33 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (466 votes). Check out past polls.