Home   Archive   Permalink



How do I dynamically produce a file path?

Hi, I am new to Rebol and am finding the language to be very exciting. Especially for the meta- programing features. I am trying to retrieve an image based on a random selection. The following code:
    
view layout [image %folder/subfolder/image.png]
    
runs with no problem and displays a small window with my image. Furthermore, the following code:
    
my-path: “%folder/subfolder/”
my-file: “image”
    
rejoin [my-path my-file “.png”]
    
produces the string
    
“%folder/subfolder/image.png”
    
that I need.
    
However, if I try:
    
view layout [image to-word rejoin [my-path my-file “.png”] ]
    
I get a window about the size of the image but without the image.
    
If I try:
    
compose [image (to-word rejoin [my-path my-file ".png"])]
    
I get the block:
    
[image %folder/subfolder/image.png]
    
which makes me think I could try and “do” this block. But if I try:
    
view layout [ do compose [image (to-word rejoin [my-path my-file ".png"])]]
    
I get:
    
** Script Error: image has no value
** Where: forever
** Near: image %folder/subfolder/image.png
    
What am I doing wrong? Is trying to assemble the path I need the way to go or should I just stick to one (long) switch statement?

posted by:   Damian       6-Jun-2012/9:49:47-7:00



You're using a string instead of a file! type:
    
my-path: “%folder/subfolder/”
    
should be
    
my-path: %folder/subfolder/
    
The REJOIN is OK because the resulting type is taken from the first part.

posted by:   Kaj       7-Jun-2012/8:28:44-7:00



Further, you need to provide the block to LAYOUT in the form it needs:
    
view layout compose [...]
    
instead of
    
view layout [compose [...]]
    
COMPOSE and others are part of the default REBOL DO dialect, not of the LAYOUT VID dialect.

posted by:   Kaj       7-Jun-2012/8:33:03-7:00



Thank you Kaj, changing from a string to a file! type seems to produce the result I was looking for and it's great news to know that REJOIN will return a file type based on the first component of the bock.
    
But the second part of your answer leads me to believe that I would not be able to use a COMPOSE instruction inside a VIEW LAYOUT [] block. Is that correct?
    
Again, thanks a million

posted by:   Damian       7-Jun-2012/8:50:19-7:00



Well, actually you don't need to compose:
    
a: %/c/program%20files/rebol/
b: %view/
c: %image.png
view layout [image rejoin [a b c]]
    
this will work too.


posted by:   Endo       11-Jun-2012/4:45:03-7:00



Damian, I thought you wanted to do something more complex with
    
view layout [ do compose [image (to-word rejoin [my-path my-file ".png"])]]
    
If you just want to compute style facets such as the image, you can indeed use regular DO expressions embedded within the VID dialect, even without using the DO keyword.
    
If you would want to construct layouts dynamically, changing style keywords, you would have to perform a COMPOSE or REDUCE before handing the block to LAYOUT. Using DO within the VID LAYOUT block only performs a normal expression, it does not yield a VID style that can be displayed.

posted by:   Kaj       11-Jun-2012/16:25:52-7:00