8

I need to reference the stem twice in the replacement for a variable substitution:

O23=$(OROOTS:%=$(ODIR)/overx-%2wk-%3wk.mlb)

I need to perform two replacements with the same stem, but the substitution uses patsubst which only does the first. How can we accomplish both?

Alexy
  • 1,520
  • 2
  • 16
  • 30

3 Answers3

10

In fact, Jack got it almost right -- foreach to the rescue! We know the full stem anyway and stick it into a var, and foreach expands all occurrences of the var:

O23 := $(foreach root,$(OROOTS),$(ODIR)/overx-$(root)2wk-$(root)3wk.mlb)

I'll check Beta's anyway for the new perspective.

Alexy
  • 1,520
  • 2
  • 16
  • 30
5

By kludgery:

O23=$(join $(OROOTS:%=$(ODIR)/overx-%2wk), $(OROOTS:%=-%3wk.mlb))
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Short of using `$(shell)`, I can think of no better option. You win... this time. – Jack Kelly Mar 08 '11 at 01:11
  • This is inconvenient if you need your two substitutions to be separated by whitespace (as join removes it); you need to add some kind of unique delimiter and replace it back with whitespace afterwards....ewww – Thomas Jun 24 '16 at 12:13
  • @Thomas: If there is whitespace between the substitution sites, the problem is *much* easier. – Beta Jun 25 '16 at 01:41
  • @Beta Indeed, I worked out a recursive join function with the word functions which works nicely. – Thomas Jun 25 '16 at 10:43
  • @Thomas: a *recursive join function?* Why not just [`subst`](http://www.gnu.org/software/make/manual/make.html#Text-Functions)? – Beta Jun 26 '16 at 01:11
0

By $(shell):

O23 := $(foreach O,$(OROOTS),$(shell echo '$(O)' | awk '{print "overx-"$$0"2wk-"$$0"3wk.mlb"}'))

I think Beta's kludgery is probably better, since it doesn't have to fork out to awk for every word in $(OROOTS).

Jack Kelly
  • 18,264
  • 2
  • 56
  • 81