Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Newbie Q. wrt Strings

 [1/8] from: sanjay::patel::xcellenet::com at: 18-May-2001 12:25


Hi, I have just started using Rebol. btw it is great! My query is if I have a string say data: "this is a test no 2" how do I manipulate the string so that I am left with the number 2. Or pull out the number 2 into another string/integer? I have tried using arrays but with no results (Arrays/Blocks). Any help would be appreciated!! SP

 [2/8] from: gjones05:mail:orion at: 18-May-2001 7:14


From: "Patel, Sanjay"
> My query is if I have a string say > data: "this is a test no 2"
<<quoted lines omitted: 3>>
> I have tried using arrays but with no results (Arrays/Blocks). > Any help would be appreciated!!
Hi, Sanjay, Welcome, to REBOL. There are several ways to slice and dice this tomato. I just throw a few out in order to give you some ideas: 1) data: "this is a test no 2" d-blk: parse data none ; yields ["this" "is" "a" "test" "no" "2"] foreach d d-blk [print d] ; yields ;this ;is ;a ;test ;no ;2 print d-blk/6 ; yields "2" type? d-blk/6 ; yields string my-integer: to-integer d-blk/6 ;yields integer type 2 through path notation my-integer: to-integer pick d-blk 6 ;yields integer type 2 through pick notation 2) data: "this is a test no 2" ; now parse as a string ;move through "no" then through ;space then copy next info to my-data ;until reach following space then parse to ;end for parse result of "true" if needed parse/all data [thru "no" thru " " copy my-data to " " to end] print my-data ; yields "2" my-int-data: to-integer my-data ; yields integer type 2 Of course, there are other ways ... If you need a different approach, please feel free to respond. --Scott Jones

 [3/8] from: al:bri:xtra at: 19-May-2001 0:14


> ...if I have a string say > > data: "this is a test no 2" > > how do I manipulate the string so that I am left with the number 2?
This:
>> last data
== #"2" will return the last character in the string. You might want to read the .pdf manual on the Rebol.com site. Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [4/8] from: carl:cybercraft at: 19-May-2001 1:00


Hi Patel, On 18-May-01, Patel, Sanjay wrote:
> Hi, > I have just started using Rebol. btw it is great! > My query is if I have a string say > data: "this is a test no 2" > how do I manipulate the string so that I am left with the number 2. > Or pull out the number 2 into another string/integer?
Strings are series, like blocks, and so you can play with them in much the same way as with blocks. Thus...
>> data: "this is a test no 2"
== "this is a test no 2"
>> last data
== #"2"
>> to-string last data
== "2"
>> n: to-integer to-string last data
== 2
>> ? n
N is an integer of value: 2 The need for the "to-integer to-string" above is because to-integer on a char! datatype returns its ASCII value. Another way to get at your number...
>> find data "no"
== "no 2"
>> pos: index? find data "no"
== 16
>> num: skip data pos - 1
== "no 2"
>> ? num
NUM is a string of value: "no 2"
> I have tried using arrays but with no results (Arrays/Blocks). > Any help would be appreciated!! > SP
-- Carl Read [carl--cybercraft--co--nz]

 [5/8] from: joel:neely:fedex at: 18-May-2001 7:47


Hi, Sanjay, There are a couple of ways I can think of immediately. Patel, Sanjay wrote:
> My query is if I have a string say > > data: "this is a test no 2" > > how do I manipulate the string so that I am left with the > number 2. Or pull out the number 2... >
Here's the demo
>> do %firstint.r >> data: "this is a test no 2"
== "this is a test no 2"
>> nodata: "there's no integer here"
== "there's no integer here"
>> find-first-int data
== 2
>> find-first-int nodata
== none
>> find-an-int data
== 2
>> find-an-int nodata
== none and here's the code 8<------------------------------------------------------------ find-first-int: func [ s [string!] /local digits nondigits partial ][ digits: charset [#"0" - #"9"] nondigits: complement digits partial: none either parse/all s [ any non-digits copy partial some digits to end ][ to-integer partial ][ none ] ] find-an-int: func [ s [string!] ][ foreach item to-block s [ if integer? item [ return item ] ] none ] 8<------------------------------------------------------------ The first version, FIND-FIRST-INT, explicitly parses the data string for a sequence of digit characters after skipping the leading non-digit characters. It's more work, but it gives a hint as to how you might handle more complex "find-the-data" tasks. The second version, FIND-AN-INT, lets REBOL do the heavy work of figuring out what's in the string. It's less work, but it depends on the fact that you're looking for something that REBOL has a pre-conceived notion about. Hope this helps! -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [6/8] from: sanjay:patel:xcellenet at: 18-May-2001 15:05


thanks. This works for me.... Sanjay

 [7/8] from: sanjay:patel:xcellenet at: 18-May-2001 15:04


thanks. This works for me.... Sanjay

 [8/8] from: carl:cybercraft at: 19-May-2001 13:33


On 19-May-01, Patel, Sanjay wrote:
> thanks. > This works for me....
<<quoted lines omitted: 5>>
> Subject: [REBOL] Re: Newbie Q. wrt Strings > Hi Patel,
Oops. Sorry Sanjay. It was rather late (read early) when I wrote that... -- Carl Read [carl--cybercraft--co--nz]

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted