|
Re: strpos [message #56729 is a reply to message #56726] |
Fri, 09 November 2007 05:43  |
Spon
Messages: 178 Registered: September 2007
|
Senior Member |
|
|
On Nov 9, 1:39 pm, Spon <christoph.b...@gmail.com> wrote:
> On Nov 9, 1:01 pm, "ben.bighair" <ben.bigh...@gmail.com> wrote:
>
>> On Nov 9, 5:24 am, greg.a...@googlemail.com wrote:
>> p2 = STRARR(N_ELEMENTS(res))
>> for i= 0L, nRes-1 do p2[i] = STRPOS(res[i], '.', p1[i])
>
> I posted this workaround and then ate it, as it was buggy when I ran
> it through a second set of strings. I still believe working around the
> problem using STREGEX and its LENGTH variable (to give you two points
> in the string) should work in principle, but my code is buggy. Sorry.
>
> RegEx = '\\[^\\]*\.'
> StrLen = STRLEN (Res)
> ExRes = STREGEX (Res, RegEx, LENGTH = Len)
> p1 = StrLen - ExRes
> p2 = StrLen - ExRes - Len
>
> Maybe someone smarter than me can work out why my p2 values are
> occasionally out by 1, occasionally not.
>
> Sorry it's only a partial solution,
> Chris
Eep, I'm using a function name as a variable! Change StrLen to
StrLength to get rid of at least one confounding factor :-)
|
|
|
Re: strpos [message #56730 is a reply to message #56729] |
Fri, 09 November 2007 05:39  |
Spon
Messages: 178 Registered: September 2007
|
Senior Member |
|
|
On Nov 9, 1:01 pm, "ben.bighair" <ben.bigh...@gmail.com> wrote:
> On Nov 9, 5:24 am, greg.a...@googlemail.com wrote:
> p2 = STRARR(N_ELEMENTS(res))
> for i= 0L, nRes-1 do p2[i] = STRPOS(res[i], '.', p1[i])
I posted this workaround and then ate it, as it was buggy when I ran
it through a second set of strings. I still believe working around the
problem using STREGEX and its LENGTH variable (to give you two points
in the string) should work in principle, but my code is buggy. Sorry.
RegEx = '\\[^\\]*\.'
StrLen = STRLEN (Res)
ExRes = STREGEX (Res, RegEx, LENGTH = Len)
p1 = StrLen - ExRes
p2 = StrLen - ExRes - Len
Maybe someone smarter than me can work out why my p2 values are
occasionally out by 1, occasionally not.
Sorry it's only a partial solution,
Chris
|
|
|
Re: strpos [message #56731 is a reply to message #56730] |
Fri, 09 November 2007 05:01  |
ben.bighair
Messages: 221 Registered: April 2007
|
Senior Member |
|
|
On Nov 9, 5:24 am, greg.a...@googlemail.com wrote:
> I'm cutting up an array of filenames, starting something like this:
>
> p1=strpos(res,'\',/reverse_search)
> p2=strpos(res,'.',p1)
>
> ...and am surprised to get this message:
>
> STRPOS: Expression must be a scalar or 1 element array in this
> context: P1.
>
> I was half-expecting to have to transpose p1, but that it's not
> allowed at all seems an omission to me. Is there any reason for that?
>
Hi,
It seems you'll have to treat the position argument as a scalar. That
means you'll need to loop through to get p2
p2 = STRARR(N_ELEMENTS(res))
for i= 0L, nRes-1 do p2[i] = STRPOS(res[i], '.', p1[i])
Bummer, eh? It seems like such a natural for the use of an array to
manage the position argument. It is either for a very good reason
(which is my bet since string handling is tricky business underneath
the hood) or it is because nobody ever thought of it. In either case,
the documentation doesn't explicitly state that position should be a
scalar, and since the expression can be it is a little misleading.
Cheers,
Ben
|
|
|