LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-07-2007, 10:05 PM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
parsing a directory path using perl


If a dir path is given like location/system/data/file

how can i store every directory name in variable
dir1=location
dir2=system
dir3=data
dir4=file
length of directory path may not be fixed.
 
Old 12-07-2007, 10:28 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
after 14 posts and a similar post like this, you should know you can use split
 
Old 12-07-2007, 11:03 PM   #3
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
i have done using split but can you tell me how to create this directory structure using perl
 
Old 12-08-2007, 05:02 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
split is a perl command!

Code:
#!/usr/bin/perl

$l_dir="/My/Dir/is/much/longer/than/yours/but/who/cares";
@l_dir_path=split(/\//, $l_dir);

for ($i=1; $i<@l_dir_path; $i++) {
   print "dir$i=$l_dir_path[$i]\n";
}
An array starts at position 0, however in this case the first entry in the array would be blank.

This is due to the / at the beginning of the string, therefore I am starting $i with a value of 1.
 
Old 12-08-2007, 06:02 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think if you told us what problem you are trying to solve, with example code, we'll be able to help you more. IOW, why would you need to do this?
 
  


Reply



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
Perl, Removing file from path to get directory only xemous Programming 8 04-11-2012 11:13 AM
PERL Parsing? ryedunn Programming 4 07-13-2006 08:48 PM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 05:31 AM
Parsing HTML using Perl smaida Programming 2 05-29-2004 01:20 PM
Perl Parsing filename The Jesus Programming 4 10-03-2003 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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