in reply to Re: Alternatives to split?
in thread Alternatives to split?

I really don't understand what you want to do.
Do you just want to extract the part up to the first delimiter?
try:
my ($tag)= split(/|/,$_,2);
my ($tag)= split(/\|/,$_,2);
This won't split on all but just the first |.

Update: Thanks to wirrwarr for correcting me

Replies are listed 'Best First'.
Re: Re: Re: Alternatives to split?
by wirrwarr (Monk) on Sep 03, 2003 at 11:31 UTC
    You have to escape the "|", otherwise you'll split at each character.
    my ($tag)= split(/\|/,$_,2);
    daniel.
      Sorry all should have added..

      Have done the split on first field
      ($tag)=split (/\|/,$data,2);
      as well as substr and index stuff
      $tag=substr ($data,0,index($data,'\|',));
      I then benchmarked them
      Benchmark: timing 1000000 iterations of split, substr... split: 8 wallclock secs ( 6.93 usr + 0.00 sys = 6.93 CPU) @ 14 +4279.32/s(n=1000000) substr: 1 wallclock secs ( 1.54 usr + 0.00 sys = 1.54 CPU) @ 64 +8088.14/s(n=1000000)
      should split be slow compared to substr?
      Thinking about what i really want to know monks...
      Once i ahve the first field is there a quick way of spliting this field on first space then on an undersocre (if it exsits)?
      In my test file I have 5161033 lines, split slows down processing dramatically. When more substr are introduced processing slows down agian!
        If the line you are splitting is very long, then yes, split will be slower than substr. To answer your question, how about something like:
        use strict; use warnings; while (<DATA>) { my $index = index($_,'|'); next unless $index; # add error checking!! my $tag = substr($_,0,$index); expensive_function($tag,$_) if $tag =~ / /; } sub expensive_function { my ($tag,$line) = @_; my ($label,$number) = split(' ',$tag,2); my ($digit) = $label =~ /_(\d+)$/; my @field = split(/\|/,$line); # now do something with all of this ... } __DATA__ START_1 123| FILE 2222| XXXX| AAAA| NEW | END_1|
        This is, of course, complete guess-work. You really should take the time to explain what it is you are trying to accompish - we know that you want to speed your application up, but what does your application do? Take the time to carefully explain yourself and get better answers. I can tell you are in a hurry because you have a plethora of typos. Take a look at How (Not) To Ask A Question.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)