in reply to Re: nth field extraction
in thread nth field extraction

This is exactly the direction I was thinking of going! Thanks!!! I will be digesting this

The problem I'm having is that I (re)process the string multiple times. That worked okay when I was doing it a few times -- perhaps several hundred or thousand times, total, in a run. But my best guess is that it will be run something between 1,500k and 3,000k times per run. Hence my hope for ideas on a better way.

Just for giggles and grins, I extracted the function I had into a standalone test script. It was probably at the top of my coding about 15+ years ago. Here it is:

#!/usr/bin/env perl my $which = 3; my $div = '!!!'; my $str = 'asdf' . $div . 'qwer' . $div . 'zxcv' . $div . 'hjkl' . $di +v . 'yuio' . $div . 'vbmn'; my @stuff; my $spot = 0; my $result = index($str, $div, $spot); print "str: [$str]\n"; while ($result != -1) { print "Found '$div' at $result\n"; my $start_spot = ($spot ? $spot + length($div) - 1 : 0); my $field_length = ($spot ? $result - $spot - length($div) + 1 : $ +result - $spot); push @stuff, substr($str, $start_spot, $field_length); $spot = $result + 1; $result = index($str, $div, $spot); } print @stuff . "\n"; print '-- #' . $which . '=' . @stuff[$which-1] . "\n";

I am continually amazed and pleased at the quality of the responses I get/see here on perlmonks! Thanks, y'all!!!

Lee Crites
lee@critesclan.com

Replies are listed 'Best First'.
Re^3: nth field extraction
by anonymized user 468275 (Curate) on Jul 30, 2018 at 14:37 UTC
    In that case there could be a slight performance benefit in storing results in a hash, e.g.
    my %res; ... ... for my $fullString (however they are obtained) { $res{$fullString} ||= fieldParse( $fullString, etc. ); etc... }

    One world, one people