CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Split fun in perl

 



Marut
New User

Dec 23, 2011, 5:25 AM

Post #1 of 5 (4734 views)
Split fun in perl Can't Post

can any one suggest me how to split the string
"my/name\is/khan\don2" and store in array using split fun in perl.

Thanks in advance.
marutmisra2009@gmail.com


FishMonger
Veteran / Moderator

Dec 23, 2011, 6:19 AM

Post #2 of 5 (4731 views)
Re: [Marut] Split fun in perl [In reply to] Can't Post

my @array = split //, $str;


Marut
New User

Dec 23, 2011, 10:16 PM

Post #3 of 5 (4688 views)
Re: [FishMonger] Split fun in perl [In reply to] Can't Post

Thanks for the reply..
But i think u din't get my question.
Actually i m trying to split a string having backslash and forward slash both.
I know backslash is the meta character.
for the other meta character i m able to split the string.
For eg.

my $string = "This ?is! a [short]-(sentence)! Why?";
my @array = split (/[\-,. :;!?()[\]{}]+/, $string);
print $_, " " foreach @array;
print "\n";
#it displays: This is a short sentence Why

Can u please help me how to split a string ""my/name\is/khan\don2" and store in array using split fun in perl.


FishMonger
Veteran / Moderator

Dec 24, 2011, 6:35 AM

Post #4 of 5 (4673 views)
Re: [Marut] Split fun in perl [In reply to] Can't Post

I knew what I posted wasn't going to do what you wanted, but it did exactly what you asked for. The point being is that you need to be clear on what you need.


Code
use Data::Dumper; 

my $str = 'my/name\is/khan\don2';
my @fields = split m~[/\\]~, $str;

print Dumper @fields;


You should start by reading the related perldocs so you can learn about regular expressions.

perlrequick Perl regular expressions quick start
perlretut Perl regular expressions tutorial
perlre Perl regular expressions, the rest of the story
perlrebackslash Perl regular expression backslash sequences
perlrecharclass Perl regular expression character classes
perlreref Perl regular expressions quick reference

It would also be a good idea to pick up a copy of "Mastering Regular Expressions"
http://shop.oreilly.com/product/9780596528126.do?green=17242849862&cmp=af-mybuy-9780596528126.IP

and/or
Regular Expressions Cookbook
http://shop.oreilly.com/product/9780596520694.do?green=17242849862&cmp=af-mybuy-9780596520694.IP


Marut
New User

Dec 24, 2011, 9:29 AM

Post #5 of 5 (4658 views)
Re: [FishMonger] Split fun in perl [In reply to] Can't Post

Thanks a lot sir ji.
This code works for me.
my personal mail id is marutmisra2009@gmail.com
can i have yours.
once again thanks..

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives