pachydermic has asked for the wisdom of the Perl Monks concerning the following question:
For example, let's suppose that my endogenous variables are:
y, c, k, a, and h.I need to take this:
y = a*(k(-1)ˆalpha)*(hˆ(1-alpha));and turn it into this:
(ySS*exp(y)) = (aSS*exp(a))*((kSS*exp(k(-1)))ˆalpha)*((hSS*exp(h))ˆ(1-alpha));Now I've already gotten started on this. Assume I already have a list of variables and equations to work with (contained in a single string which also has new lines). My code is:
my $model = "c*theta*hˆ(1+psi)=(1-alpha)*y;\n1 = beta*(c/c(+1)*alpha*y +(+1)/k+(1-delta));\ny = a*(k(-1)ˆalpha)*(hˆ(1-alpha));\nk = y-c+(1-de +lta)*k(-1);\nln(a) = rho*ln(a(-1))+ e;\n"; my @varlist = ('y','c','k','a','h'); my ($replacement,$search_string); foreach my $var (@varlist){ $search_string = quotemeta($var); $replacement = "$steady_states{$var}*exp(${var})"; $model =~ s/(\W)$search_string(\W)/$1($replacement)$2/g; $model =~ s/^$search_string(\W)/($replacement)$1/g; $model =~ s/exp\($search_string(\))+(\([\+\-]1\))/exp($search_st +ring$2$1)/g; } print $model;
And that's mostly okay except for the equation where you find "c/c(+1)" because the "/" character is already captured when the code finds "c/" - therefore "/c(+1)" isn't replaced. You can't just ignore the first (\W) because then parameters like "theta" would be problematic.
I know my regexs aren't very good anyways, so I'd like some advice about how to handle this problem.
Thank you very much for your time - hopefully I've explained everything clearly.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Help with regex - find captured pattern twice
by roboticus (Chancellor) on Apr 09, 2013 at 23:22 UTC | |
Re: Help with regex - find captured pattern twice
by hbm (Hermit) on Apr 09, 2013 at 23:37 UTC | |
by pachydermic (Beadle) on Apr 11, 2013 at 13:52 UTC | |
by hbm (Hermit) on Apr 12, 2013 at 03:11 UTC | |
Re: Help with regex - find captured pattern twice
by hdb (Monsignor) on Apr 10, 2013 at 06:55 UTC | |
by pachydermic (Beadle) on Apr 10, 2013 at 13:05 UTC | |
Re: Help with regex - find captured pattern twice
by Anonymous Monk on Apr 10, 2013 at 07:54 UTC |