split a string not only by a single char but also by a string 
Author Message
 split a string not only by a single char but also by a string

Hi,

is there a way to split a string by another string?

123<#>38923<#>hewfwiehi

->
123
38923
hewfwiehi

thanks & bye



Sun, 15 Aug 2004 10:39:09 GMT  
 split a string not only by a single char but also by a string

Quote:

> is there a way to split a string by another string?

Of course, you split on regexes usually, but that can certainly also act
as a string:

    my $string = "123<#>38923<#>hewfwiehi";


    __END__
   123 38923 hewfwiehi

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Sun, 15 Aug 2004 10:51:53 GMT  
 split a string not only by a single char but also by a string

Quote:

> Hi,

> is there a way to split a string by another string?

Yes. You'd have to use the surprisingly named "split" function.

perldoc -f split

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Sun, 15 Aug 2004 11:02:06 GMT  
 split a string not only by a single char but also by a string

Quote:

> Of course, you split on regexes usually, but that can certainly also act
> as a string:

>     my $string = "123<#>38923<#>hewfwiehi";



thanks a lot again :) this was another way I thought about, but the
other 1-line solution is much better...so far

bye



Sun, 15 Aug 2004 12:31:38 GMT  
 split a string not only by a single char but also by a string
On 27 Feb 2002 09:51:53 GMT,

Quote:

>> is there a way to split a string by another string?

> Of course, you split on regexes usually, but that can certainly also act
> as a string:

Just a clarification, because this might mislead some people, although
you probably know this:

The first argument to split is always interpreted as a regular
expression pattern [1], even if you write it with quotes or something
like that.

The following are all interpreted in exactly the same way,even though
they look differently:

Many people are surprised by this.

So, as long as your "split string" doesn't contain any special regular
expression characters, this'll work fine. otherwise, you have to make
sure to escape them, either with quotemeta or \Q (perlfunc and perlre
documentation).

Of course, it's always possible to whip up something with index and
substr (I have the vague idea that someone (not naming names) might
suggest it), but it's not going to be very Perlish,  and probably
slower than split.  The advantage of split/regexen is that it all
happens internally in perl, which is fast, while the above code does
it all in Perl, which is much slower.

Something like this:

#!/usr/local/bin/perl -w
use strict;

sub literal_split
{
    my $sep    = shift || ' ';
    my $str    = shift || $_;
    my $seplen = length $sep;

    my $old_pos = 0;
    while ((my $pos = index $str, $sep, $old_pos) > -1)
    {

        $old_pos = $pos + $seplen;
    }


Quote:
}

use Benchmark;

sub run_benchmark
{
    my $str = shift;
    my $count = $str =~ tr/ //;
    printf "String with $count separator(s), length %d\n", length $str;

    Benchmark::cmpthese(-5, {


    });
    print "\n";

Quote:
}

run_benchmark "a b ";
run_benchmark "a b " x 100;
run_benchmark "This is a string to be split";
run_benchmark "This is a string to be split" x 100;
run_benchmark "hjsadfklhjasdkfjhkjsdfhksdjfhsdjfhkasdjfsdkjasdjf ".
              "hlkjhkjlhkljhkjhkjhkjhkdfkshfkjshdfkjhsdfkdasksdfhk";
run_benchmark "hjsadfklhjasdkfjhkjsdfhksdjfhsdjfhkasdjfsdkjasdjf ".
              "hlkjhkjlhkljhkjhkjhkjhkdfkshfkjshdfkjhsdfkdasksdfhk"
              x 5000;

__END__

OUTPUT (some information removed)
String with 2 separator(s), length 4
              Rate lit_split     split
lit_split  44740/s        --      -82%
split     246398/s      451%        --

String with 200 separator(s), length 400
            Rate lit_split     split
lit_split 1140/s        --      -76%
split     4709/s      313%        --

String with 6 separator(s), length 28
              Rate lit_split     split
lit_split  24825/s        --      -77%
split     106215/s      328%        --

String with 600 separator(s), length 2800
            Rate lit_split     split
lit_split  375/s        --      -75%
split     1530/s      308%        --

String with 1 separator(s), length 101
              Rate lit_split     split
lit_split  51022/s        --      -74%
split     199816/s      292%        --

String with 1 separator(s), length 255050
           Rate lit_split     split
lit_split 121/s        --      -58%
split     287/s      137%        --

This shows more or less that the split solution will almost always win
out by a large margin, although there may be pathological cases (very
very long string with very few separators, maybe) that could reverse
the case.

Martien

[1] with some additional magic: If the pattern is omitted, split
splits on whitespace; This behaviour can be achieved as well if a
pattern of space (C<' '>, but not C</ />) is specified. See the
perlfunc documentation.
--
                                |
Martien Verbruggen              | In a world without fences, who needs
Trading Post Australia Pty Ltd  | Gates?
                                |



Mon, 16 Aug 2004 00:29:44 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

2. New bies quest

3. New bies quest

4. accessing a single char in a string

5. Splitting Strings in 80 char Portions

6. Splitting a string into an array of chars

7. changing a single quote string to a double quote string

8. Converting single quoted string to double quoted string

9. reading char by char in a string

10. Char position of 1st non-word char in a string

11. Appending char to strings by char

12. finding files containing string x but not string y

 

 
Powered by phpBB® Forum Software