Thanks guys, I could able to understand split fun.

--- On Mon, 19/7/10, John W. Krahn <jwkr...@shaw.ca> wrote:


From: John W. Krahn <jwkr...@shaw.ca>
Subject: Re: usage of Split on pattern matching
To: "Perl Beginners" <beginners@perl.org>
Date: Monday, 19 July, 2010, 4:29 PM


Chandan Kumar wrote:
> Hi all,

Hello,

> Iam beginner to perl&  i have a small query on split.
> using split i want print everything even the character used to split on 
> string.
>
> Here is my example :
>
> my $string = "hi123now456itstimeforsplit789right"
> my @array  = split ( '/(\d+\s)/ ',$string);
> ## Trying to split using digitnumber followed by space
>
> print "@array \n";
>
> please explain if anything wrong with my syntax.

You are providing to split the first argument as the string '/(\d+\s)/ ' 
which is converted to the regular expression pattern /\/(\d+\s)\/ /.  It 
sounds like you want something like:

my @array  = split /(\d+\s*)/, $string;


> My assumption is in the given string as there is no space followed by digits 
> the output of array should be empty.
>
> When I run the program ,it prints the whole string. so my assumption is wrong.
>
> please Could some one explain me on this?

split removes whatever the pattern matches.  If there is nothing to 
remove then the whole string is returned as the first element of the list.



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Reply via email to