r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Rebol School] Rebol School

Tomc
28-Jun-2007
[514x4]
>> system/options/binary-base: 16
== 16
>> as-binary "foo"
== #{666F6F}
>> system/options/binary-base: 4
== 4
>> as-binary "foo"
== #{666F6F}
>>
you cannot set any binary base ... no nibbles
nor bases higher than 16 ...
sigh
PatrickP61
28-Jun-2007
[518]
Sunanda -- Now I see what you are saying  -- Out of the 4 bytes A0 
00 A0 00, Extract did its job right by returning A0 A0 and got rid 
of the two 00!
Anton
29-Jun-2007
[519x2]
That's how it looks.
What's this "notebook" program ? You mean "notepad" (which does have 
option to save to unicode) ?
Gregg
29-Jun-2007
[521]
nor bases higher than 16 ...

 -- Except base64. I have some old base conversion code, and I think 
 Sunanda has some posted on REBOL.org as well, if you really need 
 to convert to intermediate bases.
Sunanda
29-Jun-2007
[522]
I have indeed:

http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=base-convert.r
Will handle integer <--> base conversions.
Up up base 36 out of the box
Up to base 255 if you adjust the configurable parameters:

http://www.rebol.org/cgi-bin/cgiwrap/rebol/documentation.r?script=base-convert.r#toc-19
PatrickP61
29-Jun-2007
[523]
my mistake  -- I mean Notepad  -- not Notebook
Anton
29-Jun-2007
[524]
:) ok
PatrickP61
2-Jul-2007
[525x4]
Question to all:   

If I have a block of data inside of In-text like this:
	Line A
	Line B
	Line C

How can I print the line number (position in the block) along with 
the contents of the line?               I tried this but it didn't 
work:
foreach Line In-text [ print rejoin	[ Count:	Count + 1	]	Line ]
Now that I think of it, I probably do not need to manuipulate a Count 
variable  -- I can probably use INDEX right?
I tried this out but not getting the results I wanted:

	Data:	head In-text
	while	[not tail? Data] [
			print	[index? Data Data ]
			Data:	next Data		]

I'm getting this:
	1 Line A Line B Line C
	2 Line B Line C
	3 Line C
Any suggestions?
Give me enough time, and I will figure it out --- :-)

Data:	head In-text
while	[not tail? Data] [
		print	[index? Data first Data ]
		Data:	next Data		]

Is there a better way to code this kind of thing?
Sunanda
2-Jul-2007
[529]
One way:
data: [a b c]
for n 1 length? data 1 [print [n data/:n]]
Brock
2-Jul-2007
[530x3]
>> blk: ["First line of data" "Second line of data" "Third line of 
data"]
>> while [not tail? blk][ print [index? blk first blk] blk: n
ext blk]
1 First line of data
2 Second line of data
3 Third line of data
blk: [
first line
second line
third line
]

while [not tail? blk][ print [index? blk first blk] blk: next blk]
Your first answer seems to work for me
PatrickP61
2-Jul-2007
[533x3]
My first attempt had   print [index? Data Data]   while the second 
attempt has   print [index? Data first Data]
The second one got the right part of the series
Sunanda  --  I like to see how to solve the same problem in different 
ways   thanks for the reply
Ashley
3-Jul-2007
[536]
for n 1 length? data 1
 -> "repeat n length? data"
PatrickP61
5-Jul-2007
[537]
Situation:	I want to read in an input file and parse it for some 
strings

Current:	My test code will do the parsing correctly IF the input 
block contains each line as a string

Problem:	When I try to run my code against the test file, It treats 
the contents of the file as a single string.

Question:	How do I have Rebol read in a file as one string per line 
instead of one string?
In-text:	[	"Line 1                        Page     1"
		"Line 2    Name      String-2"          
		"Line 4    Member    String-3 on 12/23/03"
		"Line 5    SEQNBR    abcdef               "                
		"Line 6       600    Desc 1 text         12/23/03"
		"Line 7      5400    Desc 2    Page 4    12/23/03"
		"Line 8    Number of records searched	]
 Get-page:		[thru "     Page "	copy Page-id to end]
 Get-file:		[thru "Name  "		copy Name-id to end]
 Get-member:	[thru "Member  "	copy Member-id to end]

 Page-id:	Name-id:	Member-id:	"-"

 for N 1 length? In-text 1 [
	parse In-text/:N	Get-page
	parse In-text/:N	Get-file
	parse In-text/:N	Get-member
	] 
 print	[ "Page"	Page-id		]
 print	[ "Name"	Name-id		]
 print	[ "Member"	Member-id	]
Sunanda
5-Jul-2007
[538]
Try
  in-text: read/lines %file-name
PatrickP61
5-Jul-2007
[539x2]
Thank you Sunanda -- That did work, but I thought Read/Lines would 
return a single line  -- no maybe that is Read/Line without the s 
 -- is that right?
In my example above, I have three parse rules defined.  I need to 
add several more.

Does the PARSE process the string once per rule?  i.e. Does it scan 
the string for Get-page, then Get-file, then Get-member (scan the 
string 3 times),  Or can I structure the pase rules together to process 
against the string once?
Tomc
5-Jul-2007
[541x2]
if your page,name & member always exist and are in that order ...

parse/all read %file [
 some [ 
	thru "Page " copy token integer! (print ["Page" token]) 
	thru "Name " copy token to newline(print ["Name" token])
	thru "Member " copy token to newline (print ["Member" token])
	]
]
snd the keywords only exist as key words
PatrickP61
5-Jul-2007
[543x2]
Tomc  -- This version means that I need to have the entire file read 
in as a string -- Not with Read/Lines -- Because the newline will 
the the "delimiter" within the string while the Read/Lines will delimit 
each newline to a separate string inside a block.  Do I have that 
right?
My Page, Name, & Member is always in the same order on separate pages 
within a file.  like so:
Line 1     Page 1
Line 2     Name
Line 3     Member
Line n...  Member
Line 50  Member
Line 51  Page   2
Line 52  Name
Line 53  Member
Line 54  Member
...
Sunanda
6-Jul-2007
[545]
Not sure this is a case for parse......You seem to have four types 
of line:
-- those with "page" in a specific location on the line
-- those with "name" in a specific location on the line
-- those with "member" in a specific location on the line

-- others which are to be ignored .... eg your orginal line 6 "Line 
6       600    Desc 1 text         12/23/03"

What I would do is:
* use read/lines to get a block

* for each line in the block, identify what record type it is by 
the fixed literal .... something like: if "page" = copy/part skip 
line 25 4 [....]

* perhaps use parse to extract the items I need, once I know the 
line type
***

If you just use parse in the way you propose, you run the risk of 
mis-identifying lines when there is a member called "page" or "name"
PatrickP61
6-Jul-2007
[546x4]
Thank you Sunanda -- I will give that a try.


Just to let you know -- My goal is to convert a printable report 
that is in a file into a spreadsheet.
Some fields will only appear once per page like PAGE.

Some fields could appear in a new section of the page multiple times 
like NAME in my example.
And some fields could appear many times per section like MEMBER:
_______________________
Page header          PAGE     1
Section header     NAME1.1
Detail lines            MEMBER1.1.1
Detail lines            MEMBER1.1.2
Section header     NAME1.2
Detail lines            MEMBER1.2.1
Detail lines            MEMBER1.2.2
Page header         PAGE    2
(repeat of above)____________


I want to create a spreadsheet that takes different capturable fields 
and place them on the same line as the detail lines like so...
______________________
Page   Name       Member
1          NAME1.1  MEMBER1.1.1
1          NAME1.1  MEMBER1.1.2
1          NAME1.2  MEMBER1.2.1
1          NAME1.2  MEMBER1.2.2

2          NAME2.1  MEMBER2.1.1  ...    (the version numbers are 
simply a way to relay which captured field I am referring to (Page, 
Name, Member)


Anyway -- that is my goal.  I have figured out how to do the looping, 
and can identify the record types, but you are right about the possiblity 
of mis-identifying lines.
This is my pseudocode approach:


New page is identified by a page header text that is the same on 
each page and the word PAGE at the end of the line

New section is identified by a section header text that is the same 
within the page and the text "NAME . . . . :"

Members lines do not have an identifying mark on the line but are 
always preceeded by the NAME line.

Member line continue until a new page is found, or the words "END 
OF NAME" is found (which I didnt show in my example above).


Initialize capture fields to -null-     like PAGE, NAME
Initialize OUTPUT-FLAG to OFF.

Loop through each line of the input file until end of file EOF.
/|\	If at a New-page line
 |	or at end of Name section
 |		Set OUTPUT-FLAG  OFF
 |	If OUTPUT-FLAG  ON

 |		Format output record from captured fields and current line (MEMBER)
 |		Write output record
 |	IF at New Name line
 |		Set OUTPUT-FLAG ON
 |	IF OUTPUT-FLAG OFF
 |		Get capture fields like PAGE-NUMBER when at a PAGE line
 |		Get NAME when at a NAME line.
 |____	Next line in the file.
Note to all -- Please realize this is a simplified version of the 
real report -- There are many more fields and other things to code 
for, but they are all similar items to the example PAGE, NAME, and 
MEMBER fields.
Oops -- I should put the IF at New Name line at the end of the loop, 
or put the capture of the name in that part.
Tomc
7-Jul-2007
[550]
Yes Patrick you have it right. The rules I gave would fail 
since you have multiple names/members

I would try to get away from the line by line mentality 
and try to break it into your conceptual record groupings
file, pages, sections, and details...

One trick I use is to replace a string delimiter for a record 
with a single char so parse returns a block of that record type. 

this is good because then when you work on each item in the block 
in turn
you know any fields you find do belong to this record and that 

you have not accidently skipped to a similar field in a later record.

something like this 


pages: read %file
replace/all/case pages "PAGE" "^L"
pages: parse/all pages "^L"

foreach page pages[
	p: first page
	page: find page newline
	replace/all/case page "NAME" "^L"
	sections: parse page "^L"
	foreach sec section [
		s: first section
		sec: find sec newline
		parse sec [
			any [thru "Member" copy detail to newline 
				newline (print [p tab s tab detail])
			]
		]
	]
]
PatrickP61
18-Jul-2007
[551x4]
I am a little confused about PORTS.  I want to control how much information 
is loaded into a block but I am not sure how to determine if data 
remains in a port.  Example:
In-port:	open/lines In-file
 while [not tail? In-port] [
	print In-port
	In-port:	next In-port
	]

 close In-port
This is not doing what I want.  

I want it to continue to run through all  lines of a file and print 
it
My goal is to be able to control how much of a file is loaded into 
a block then process the block and then go after the next set of 
data.  That is why I am using PORT to do this function instead of 
reading everything into memory etc.
Geomol
18-Jul-2007
[555x2]
Change the print line to:
print first In-port
I think, your code print the port specs and everything.
PatrickP61
19-Jul-2007
[557]
Yes, It does dump a lot of stuff that I don't kow about!!!
btiffin
19-Jul-2007
[558x2]
Ports are nifty little objects.  :)  And if you just type
>> In-port
 you get back nothing, just another prompt.
>>

The interpreter does not display the internals of objects, but print 
does, so what you are seeing is the object! that is In-port.  Well, 
I'm lying...In-port is a port!  not an object!  Close but not the 
same.  ports emulate a series! wrapped in object! wrapped in enigma. 
 Or is it an object! wrapped in a series! disguised as a sphynx? 
 :)


first In-port is a REBOL reflective property feature that when you 
get it, you'll go "Ahhhh" as you step closer to the Zen of REBOL.

For fun with a really big object!  try >> print system
Oh, by the way...we added to the %form-date.r script in the library. 
  See I'm New for details.
PatrickP61
20-Jul-2007
[560]
Another question -- I know to use escape to insert things like a 
tab as in ^(tab) into a string.
What can I use to insert a newline?  ^(newline) doesn't work.
Geomol
20-Jul-2007
[561x3]
str: ""
insert str newline
If you just write NEWLINE in the prompt, you'll see how it's defined. 
You can specify a newline in a string as
str: "a string with a newline: ^/"
A tab can also be specified as: "^-"