Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-27-2005, 11:23 PM   #1
LQ Newbie
Registered: Dec 2004
Location: I live here
Posts: 3

Rep: Reputation: 0
Question how to seperate filename from path in perl

I'm sure this is very simple but I am not much of programmer.

I am trying to better some of my perl scripts. One thing that would greatly help my current script is some what to separate the filename from the rest of the path for a given path+file name. Here's an example:
I'd provide let's say /user/me/data/cars.dat into my perl program. I want to be able to separate the cars.dat and assign it a variable and the rest of the path (/user/me/data) a second variable so that I can rename the filename to modifiedcars.dat and copy it back to the to the original path once I've modified the data in the program (i.e. output to /user/me/data/modifiedcars.dat). I see two possible ways to do this:

1) Use the spilt command with / as the delimiter and try to catch the last segment but I'm not sure how to do this and how I would be able to assign the rest of the path to variable (as far as I see it).

2) Do some pattern matching using something like $filename = //(w+)/g[-1]; which I'm also sure is wrong. I'm also not sure how I would assign the rest of the path a variable name either.

I'm sure this is very simple to do but can't seem to figure it out based on the texts I have and the reading I've done on the internet. Any help would be greatly appreciated.
Old 08-27-2005, 11:31 PM   #2
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Chapter 9.10 of the Perl Cookbook addresses this in some depth.

I recommend this book (along with Programming Perl) to anyone who wants to really master perl.
Old 08-28-2005, 01:35 AM   #3
LQ Newbie
Registered: Dec 2004
Location: I live here
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Puffinman,
I feel like a real idiot now because I do have that book (and learning perl) but I was thinking it would be someting done via the pattern matching chapter (6). Anyway, I appreciate you setting me straight.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
filename truncation Perl belmer2 Programming 1 04-24-2005 01:31 PM
how do i get the path of the filename linuxzouk Programming 25 06-10-2004 08:00 AM
Perl Scripting on Filename lengths supreme_command Programming 2 05-16-2004 03:06 AM
Perl Parsing filename The Jesus Programming 4 10-03-2003 03:21 PM
path and filename of compressed kernel ? ohernandez Linux - General 3 05-29-2002 06:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration