MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extracting information from lists


Tony,

To extract the positions of the first element of runs of a given
integer within a list, a very simple approach is to let a regex do all
the dirty work:

In[23]:=
posFn[x:{__Integer},elem_Integer]:=
    First/@StringPosition[StringJoin[ToString/@x],
        RegularExpression[ToString[elem]<>"+"],Overlaps->False];
In[25]:=
posFn[{11791681165111876111171},1]
Out[25]=
{1,5,8,12,18,23}
In[26]:=
posFn[{79168116511187611117},1]
Out[26]=
{3,6,10,16}
In[27]:=
posFn[{79168116511187611117},8]
Out[27]=
{5,13}

An equivalent formulation using Mathematica string expressions would
be:

posFnAlt[x:{__Integer},elem_Integer]:=
    First/@StringPosition[StringJoin[ToString/@x],ToString[elem]..,
        Overlaps->False];

dkr


  • Prev by Date: Re: Re: Replacement equivalence?
  • Next by Date: Re: Extracting information from lists
  • Previous by thread: Re: Extracting information from lists
  • Next by thread: Re: Extracting information from lists