Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: problems with delayed write: tag list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79901] Re: problems with delayed write: tag list
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 8 Aug 2007 04:54:10 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f991i3$c99$1@smc.vnet.net>

P_ter wrote:
> Hello,
> I have a list of elements of two numbers:myList = {{1,2},{2,7},..}. The first number is in this case always the position. I want to know when the difference is a certain value,myDiff, eg. 5
> i=1;myDiff==5;
------------^^
Error: must be a single equal sign = to make an assignment; otherwise it 
is a logical comparison returning True or False.

> now= Reap[While[i<Length[myList],i++;
>        If[myList[[i,2]]-myList[[i,1]]==myDiff,Sow[i]]];w]
---------------------------------------------------------^
Q: What is w? Below, I shall assume that it is the current element, so 
that you wanted something like a lit of {position, pair}.

> My question is: how do I make "nowFunction[myDiff_]:="? 
> I run into tag list errors in the definition when I try that.

In[1]:= myList = {{1, 2}, {2, 7}, {3, 5}, {4, 9}};
Clear[nowFun]
nowFun[lst_List, delta_Integer] :=
  Module[{i = 0, len = Length@lst},
   Flatten[Reap[
      While[i++ < len,
       If[lst[[i, 2]] - lst[[i, 1]] == delta, Sow[{i, lst[[i, All]]}]
        ]
       ]
      ][[2]], 1]
   ]
nowFun[myList, 5]

Out[4]= {{2, {2, 7}}, {4, {4, 9}}}

Note that it might be worth investigating some others more efficient, 
less error-prone approaches (depending, of course, of your real needs) 
such as

In[5]:= myDiff = 5
Select[myList, #[[2]] - #[[1]] == myDiff &]
Cases[myList, x_ /; x[[2]] - x[[1]] == myDiff]
Cases[myList, {x_, y_} /; y - x == myDiff]
Position[myList, {x_, y_} /; y - x == myDiff]

Out[5]= 5

Out[6]= {{2, 7}, {4, 9}}

Out[7]= {{2, 7}, {4, 9}}

Out[8]= {{2, 7}, {4, 9}}

Out[9]= {{2}, {4}}

Regards,
Jean-Marc


  • Prev by Date: Re: how to collapse a cell in v.6
  • Next by Date: show residuals of circle fit
  • Previous by thread: Re: problems with delayed write: tag list
  • Next by thread: Re: problems with delayed write: tag list