Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split() on /

Status
Not open for further replies.
Aug 14, 2008
38
US
I am trying to read a string and have it split into pieces using the split() function. I am using the "/" as the separator to split on, but not sure how to write the split to use the "/".

Input is going to be directory listings like below

/usr/home/<userid>/Trash
/usr/home/<userid>/Sent
/usr/home/<userid>/Trash
/usr/home/<userid>/mbox


I have been trying to do it using the following line...
Code:
($lvl1, $lvl2, $userid, $filename)= split(///);


Thank you for taking time to read this question
 
Hi

Perl:
[teal]([/teal][highlight][navy]$lvl0[/navy][teal],[/teal][/highlight] [navy]$lvl1[/navy][teal],[/teal] [navy]$lvl2[/navy][teal],[/teal] [navy]$userid[/navy][teal],[/teal] [navy]$filename[/navy][teal])=[/teal] [b]split[/b][teal]([/teal][green][i]/[highlight]\[/highlight]//[/i][/green][teal]);[/teal]

Feherke.
 
You can also use different delimiters for regular expressions. Instead of "//", it is also common to use "{}" in order to avoid escaping the special "/" character. Parentheses are optional with split:

Code:
($lvl1, $lvl2, $userid, $filename)= split {/};
 
Hi

Interesting. Your code not works in my Perl 5.10.1 :
Code:
  [blue]DB<1>[/blue] $_='/usr/home/<userid>/Trash';

  [blue]DB<2>[/blue] ($lvl1, $lvl2, $userid, $filename)= split {/};
Search pattern not terminated at (eval 6)[/usr/lib/perl5/5.10.1/perl5db.pl:638] line 2.
 at (eval 6)[/usr/lib/perl5/5.10.1/perl5db.pl:638] line 2
        eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D = $^D | $DB::db_stop;
($lvl1, $lvl2, $userid, $filename)= split {/};;

;' called at /usr/lib/perl5/5.10.1/perl5db.pl line 638
        DB::eval called at /usr/lib/perl5/5.10.1/perl5db.pl line 3444
        DB::DB called at -e line 1
I what kind of Perl can you use that code ?

Feherke.
 
Oops! I forgot to add the "m".

Code:
($lvl1, $lvl2, $userid, $filename)= split m{/};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top