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

MathGroup Archive 1998

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

Search the Archive

Re: ReplacePart?



Scott Morrison wrote:
> 
> I have a list of pairs of the form:
> gt={{3,5},{{},6},{7,{}},{{},{}},{{},56},{{},3}};
> 
> The empty lists inside pairs are being used as placeholders. I want to
> replace these with the elements of b: b={a1,a2,a3,a4,a5,a6};
> hopefully resulting in the list
> {{3,5},{a1,6},{7,a2},{a3,a4},{a5,56},{a6,3}}
> 
> I had thought that ReplacePart could do this, as it says:
> "ReplacePart[expr, new, pos, npos] replaces parts at positions pos in
> expr by
>   parts at positions npos in new."
> 
> I can't get this to work at all. If someone could explain how to do this
> sort of thing, using ReplacePart or otherwise, I would be most
> grateful.
> 
> Scott Morrison
> scott@morrison.fl.net.au

Scott:

gt={{3,5},{{},6},{7,{}},{{},{}},{{},56},{{},3}};

b={a1,a2,a3,a4,a5,a6};

n=1;gt//.{}:> b[[n++]]

{{3,5},{a1,6},{7,a2},{a3,a4},{a5,56},{a6,3}}

The following gives more control

n=1;Apply[(gt[[##]] = b[[n++]])&,Position[gt,{}],{1}];

-- 
Allan Hayes
Training and Consulting
Leicester, UK
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642




  • Prev by Date: Re: Questions on MultipleListPlot: One answered, one still troubling
  • Next by Date: Re: List Manipulation
  • Prev by thread: Re: ReplacePart?
  • Next by thread: Left and right quotes