How to extract a substring from a string?

nnCron and nnCron LITE discussion

How to extract a substring from a string?

Postby billox » Thu, 07 Apr 2011, 21:35

Ok, new question :-)

I have a variable contains a path that I get from Windows registry.
The key contains .bmp file's path.

Code: Select all
...
CREATE reg_BmpName 256 ALLOT
reg_BmpName GET-REG: "HKEY_USERS\%user1SID%\Software\MySoftware\LastBmp"
...

Now, I want to show a hint (for 10 secs) with new image's name.
Code: Select all
THINT: "The new image is:%crlf%%reg_BmpName ASCIIZ>%" 10

The problem is that it show entire path of the file and not only the name.
For example, it will show:
    C:\Documents\Bmps_For_me\Very_good_Image.bmp
But I'd like to see:
    Very_good_Image.bmp

Now, can I extract substring Very_good_Image?
How I can count for 27 chars and then extract the rest of the string?
The name start always from 28° char.

I hope I made clear myself. :-)
Thanks.
billox
 
Posts: 9
Joined: Sat, 02 Apr 2011, 19:18

Re: How to extract a substring from a string?

Postby SeMa » Fri, 08 Apr 2011, 12:43

Code: Select all
...
reg_BmpName COUNT S" /.+\\(.+\.bmp)$/" RE-MATCH
IF
    THINT: "The new image is:%crlf%%$1%" 10
THEN
...
User avatar
SeMa
 
Posts: 637
Joined: Fri, 15 Apr 2005, 12:49
Location: Украина

Re: How to extract a substring from a string?

Postby billox » Fri, 08 Apr 2011, 18:40

SeMa wrote:
Code: Select all
...
reg_BmpName COUNT S" /.+\\(.+\.bmp)$/" RE-MATCH
IF
    THINT: "The new image is:%crlf%%$1%" 10
THEN
...

Thank you very much, SeMa! It works like a charm! :-)

Last question: if I have *.bmp and *.jpg?
How can I do in this manner (in my pseudo-code :wink: :, sorry) ?

reg_BmpName COUNT S" /.+\\(.+bmp OR jpg)$/"RE-MATCH
billox
 
Posts: 9
Joined: Sat, 02 Apr 2011, 19:18

Re: How to extract a substring from a string?

Postby SeMa » Sat, 09 Apr 2011, 18:13

billox wrote:reg_BmpName COUNT S" /.+\\(.+bmp OR jpg)$/"RE-MATCH

(.+(bmp)|(jpg))
nnCron help - nnCron Key Words - Regular Expressions Support
nnCron help - Additional info - Regular Expressions
Компьютер ─ устройство, разработанное для ускорения и автоматизации человеческих ошибок
User avatar
SeMa
 
Posts: 637
Joined: Fri, 15 Apr 2005, 12:49
Location: Украина

Re: How to extract a substring from a string?

Postby billox » Sat, 09 Apr 2011, 19:44

Ok, thanks again for your patience.
I offer you a virtual beer. :partyman:

I read two links. I promise learned more.
Sorry, but Forth, for me, is a bit difficult...

---EDIT
Ok, I modify a little my script.
I want to eliminate the extension (bmp or jpg) and eliminate progressive number at the beginner of my image's files.
Code: Select all
 reg_BmpName COUNT S" /.+\\(.+\.bmp)|(.+\.jpg)$/" RE-MATCH
IF
S" %$1%" EVAL-SUBST sTemp PLACE
sTemp COUNT 3 /STRING sTemp PLACE
sTemp COUNT 4 - 0 MAX sImage PLACE
 S" Georgia" 16 HINT-FONT
 0x245DDB 0xFFFF00 HINT-COLOR
   THINTW: "My image is:%crlf%%sImage ASCIIZ>%" 15
 THEN

It works fine. But there is only a little problem.

The hint has a little square in front of the string.
See it:
Image
Why is there this "incorrect" character?
I tried to use MSG: and appears a little sign instead of the square.
billox
 
Posts: 9
Joined: Sat, 02 Apr 2011, 19:18

Re: How to extract a substring from a string?

Postby billox » Sun, 10 Apr 2011, 21:52

Ok, I absolute beginner, but as promise, I tried many times in these two evenings.

Finally, I managed to run my script. :mrgreen:
Code: Select all
reg_BmpName COUNT S" /.+\\(.+\.bmp)|(.+\.jpg)$/" RE-MATCH
IF
S" %$1%" EVAL-SUBST sTemp PLACE
sTemp COUNT 3 /STRING sTemp PLACE
sTemp COUNT 4 - 0 MAX sImage ZPLACE
S" Georgia" 16 HINT-FONT
0x245DDB 0xFFFF00 HINT-COLOR
   THINTW: "My image is:%crlf%%sImage ASCIIZ>%" 15
THEN


But don't really know why it works, :rolleyes:
Obviously, the trick was to use ZPLACE in the last assignment.

But curiously, I didn't find any reference to ZPLACE in nnCron documentation.
I only found a reply of Nicholas_Nemtsev on this forum (in this thread).
But perhaps I wasn't able to find it.

Anyway, thanks to your support SeMa. :yawinkle:
billox
 
Posts: 9
Joined: Sat, 02 Apr 2011, 19:18

Re: How to extract a substring from a string?

Postby SeMa » Mon, 11 Apr 2011, 17:45

billox wrote:THINTW: "My image is:%crlf%%sImage ASCIIZ>%" 15

THINTW: "My image is:%crlf%%sImage COUNT%" 15

There are several (2 minimum) ways string process in Forth. I myself still do not quite understand the mechanism of their action. But it is.
Компьютер ─ устройство, разработанное для ускорения и автоматизации человеческих ошибок
User avatar
SeMa
 
Posts: 637
Joined: Fri, 15 Apr 2005, 12:49
Location: Украина

Re: How to extract a substring from a string?

Postby billox » Mon, 11 Apr 2011, 19:25

Oh, I see.
Unfortunately Forth language is not very common and hard to find examples on which to learn.
What a pity, because I found Forth very powerful.

For example, what the mean of "0=" at the end of this portion of script?
Code: Select all
...
FILE-EXIST: "C:\Docs\Mydoc.txt" 0=
IF
...


I looked at the sites suggested in the nnCron Help, but I have not yet found a comprehensive site for beginners on the Forth language.
billox
 
Posts: 9
Joined: Sat, 02 Apr 2011, 19:18

Re: How to extract a substring from a string?

Postby SeMa » Tue, 12 Apr 2011, 17:41

FILE-EXIST: "C:\Docs\Mydoc.txt" 0=
equals
FILE-EXIST: "C:\Docs\Mydoc.txt" NOT
billox wrote:I looked at the sites suggested in the nnCron Help, but I have not yet found a comprehensive site for beginners on the Forth language.

http://www.google.com.ua/search?q=Thinking+Forth
http://www.google.com.ua/search?q=Forth+literature
search!..
Компьютер ─ устройство, разработанное для ускорения и автоматизации человеческих ошибок
User avatar
SeMa
 
Posts: 637
Joined: Fri, 15 Apr 2005, 12:49
Location: Украина

Re: How to extract a substring from a string?

Postby ricaluanna » Mon, 13 Jun 2011, 04:59

Thanks for sharing this one.
ricaluanna
 
Posts: 1
Joined: Mon, 13 Jun 2011, 04:11


Return to nnCron forum (English)

Who is online

Users browsing this forum: No registered users and 0 guests