replacing text in a string

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

jbonanca
Posts: 33
Joined: Mon Dec 06, 2010 1:49 pm

replacing text in a string

Postby jbonanca » Mon Aug 10, 2015 12:30 pm

Hi,

Could someone help me understand why I'm failing at this?

I have a string with these contents:

_GDP_STUFF_158
_GDP_STUFF_20
_CPI_STUFF_120
_CPI_STUFF_1

I'd like to make a new string consisting only of two elements: "_GDP_STUFF_" and "_CPI_STUFF_". My strategy was to use @wreplace to replace the numbers with nonsense that I would then use @wdrop to get rid of, and then use @wunique to get me only unique elements.

If anyone has a more elegant way they've done something similar, please let me know. But outside of that, I am failing trying to get @wreplace to work.

Assuming the name of mu string is "stringname," I am trying things like:

string stringname=@wreplace(stringname,"_*_*_*","_*_*_00")

to no avail.

Anyone have any guidance? Thank you!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13319
Joined: Tue Sep 16, 2008 5:38 pm

Re: replacing text in a string

Postby EViews Gareth » Mon Aug 10, 2015 1:06 pm

You can't replace a wildcard with something.

In your examples, I think something like:

Code: Select all

create u 10

string stringname = "_gdp_stuff_158"

string stringname1 = @left(stringname, @instr(stringname, "_", 3)) + "00"

show stringname1

should work
Follow us on Twitter @IHSEViews

jbonanca
Posts: 33
Joined: Mon Dec 06, 2010 1:49 pm

Re: replacing text in a string

Postby jbonanca » Mon Aug 10, 2015 1:33 pm

thank you

jbonanca
Posts: 33
Joined: Mon Dec 06, 2010 1:49 pm

Re: replacing text in a string

Postby jbonanca » Mon Aug 10, 2015 1:47 pm

Forgive me Gareth but I just realized it won't work. I posed my problem incorrectly; apologies.

My starting string is this: "_GDP_OLDSTUFF_18 _GDP_OLDSTUFF_122 _GDP_NEWSTUFF_50"

and the output i need is this string:

"_GDP_OLDSTUFF_ _GDP_NEWSTUFF_"

so what i need is just way to drop the numbers and retain every unique element once I ignore the numbers.

if I use your approach i get
"_GDP_OLDSTUFF_00" -- so I lose "_GDP_NEWSTUFF_"

sorry to follow up on it, but this is surprisingly vexing. if you have any other ideas i would be very grateful!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13319
Joined: Tue Sep 16, 2008 5:38 pm

Re: replacing text in a string

Postby EViews Gareth » Mon Aug 10, 2015 1:55 pm

Loop through the words of your string using my replacement on each word one at a time.
Follow us on Twitter @IHSEViews

jbonanca
Posts: 33
Joined: Mon Dec 06, 2010 1:49 pm

Re: replacing text in a string

Postby jbonanca » Mon Aug 10, 2015 2:10 pm

haha. oh boy. it is clearly miller time ... thanks gareth... got a little stuck there! :)


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 48 guests