guha has asked for the wisdom of the Perl Monks concerning the following question:
However, I find the solution(s) slightly unperlish and thought I would get some feedback from you.
Basically I found that the index function does not DWIM in array context, like for example my @offset = index($txt, $srch);
Below are two variations I came up with that works to large extent, I think.
As you can see I would like it to work under strict, preferably be a "one-liner" and not require additional/temporary variables.#!perl -w use strict; my $txt = 'Trying! to get! index functionality! in array context!?'; my $srch = '!'; my @offset; my $l = -1; @offset = map {$l += length($_) + 1 } $txt =~ /(.*?)(?:$srch)/g; print "RegExp: ", join(", ", @offset), "\n"; @offset =(); my $ofs = 0; while( ($ofs = index($txt, $srch, $ofs)) > 0 ) { push @offset, $ofs++; } print "Index : ", join(", ", @offset), "\n";
TIA
---
It's unfair to be an expert.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 'Trying! to get! index functionality! in array context!?'
by Kanji (Parson) on Jan 28, 2002 at 15:55 UTC | |
by Anonymous Monk on Jan 28, 2002 at 16:30 UTC | |
|
Bones! Take a reading . . .
by Fletch (Bishop) on Jan 28, 2002 at 20:22 UTC | |
by merlyn (Sage) on Jan 28, 2002 at 22:04 UTC |