in reply to Removing digits until you see | in a string

To get whatever's before the |:

($data_hash{$index}) = $str =~ /([^|]*)/;

( I originally posted ($data_hash{$index}) = $str =~ /(\d+)/;. )

Replies are listed 'Best First'.
Re^2: Removing digits until you see | in a string
by kevyt (Scribe) on Jan 08, 2007 at 05:29 UTC
    When I do it this way, it seems to overwrite the previous index in the hash. I store about 40 indexes with strings but I only had one to print. I am not sure what I did wrong.
      Sorry, I misread the problem. Use quester's.
Re^2: Removing digits until you see | in a string
by jettero (Monsignor) on Jan 08, 2007 at 14:23 UTC

    See, I would have used something like ($data_hash{$index}) = $str =~ m/^(.+?)(?=\|)/. I wondered if it was faster to stop on the pipe with [^|] or use a lookahead:

    use strict; use Benchmark; my $index; my $str = "lol238923892382938|lol282812|asdfasdf|asdfasdfasdf"; timethese(5000000, { 'stopper' => sub { ($index) = $str =~ m/([^|]+)/ }, 'lookahead' => sub { ($index) = $str =~ m/^(.+?)(?=\|)/ }, 'splitter' => sub { ($index) = split /\|/, $str }, });

    The lookahead is technically faster on my machine, but not by enough to count as a victory. I'd be curious about other's results. Sadly, the splitter wins over the regulars by a similar (i.e. smallish) amount.

    -Paul