Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Replace list element based on a condition how to

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106658] Re: Replace list element based on a condition how to
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Wed, 20 Jan 2010 06:49:27 -0500 (EST)

On 1/19/10 at 5:13 AM, canopus56 at yahoo.com (Canopus56) wrote:

>Q1. Is there a way to replace a list based on a condition?

>I have a list of integers -

>lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3}

>that I would like to replace with the following rule -

>fncHourFix[h_] := If[(h - 6) < 0, h - 6 + 24, h - 6];

>but the replace function -

>lst2 = lst1 /. {i_} :> fncHourFix[{i}]

>- bombs out.
>I've tried several variants. Any pointers would be appreciated.

The problem is the pattern {i_}. Change this to i_Integer and
things work fine. That is

In[4]:= lst1 /. n_Integer :> fncHourFix[n]

Out[4]= {12,13,14,15,16,17,18,19,20,21}



  • Prev by Date: simple nest
  • Next by Date: Re: Replace list element based on a condition how to
  • Previous by thread: Re: Re: Re: Replace list element
  • Next by thread: Re: Replace list element based on a condition how to