URL join question
Newbie here! I am trying to download an image file 10 times and give it sequential names. How do I substitute the number 3 in the script below with the i from the loop ? repeat i 10 [write %3.png read/binary http://www.rebol.com/graphics/reb-logo.gif]
posted by: adam 9-Nov-2017/4:20:39-8:00
Hi Adam! It's pretty easy: repeat i 10 [write to file! rejoin [i %.png] read/binary http://www.rebol.com/graphics/reb-logo.gif]
posted by: rebolek 9-Nov-2017/5:03:25-8:00
thanks it worked! what's the difference between write and write to file ?
posted by: adam 9-Nov-2017/5:12:19-8:00
rejoin ["1" %pgn] creates a string, not a file name. to-file "..." converts the string to a file name.
posted by: Sunanda 9-Nov-2017/7:09:28-8:00
In this case, the 'write function requires a file! data type, instead of a string! data type, which is currently generated by the 'rejoin function. You can use 'to-file to create the file type, or you could also coerce 'rejoin to output a file type by making the first value in the rejoined block a file type. This also enables you to specify where the file is saved: repeat i 10 [write rejoin [%./ i %.png] read/binary http://www.rebol.com/graphics/reb-logo.gif] Also, you should usually use 'write/binary if you're using 'read/binary. Additionally, be aware that you're currently writing a .gif file to a new file name that ends in .png. In many image applications, this can cause an error. Do this instead: repeat i 10 [write/binary rejoin [%./ i %.gif] read/binary http://www.rebol.com/graphics/reb-logo.gif] If in fact you actually want to convert the .gif image to a .png image, use 'load to convert the downloaded image to a Rebol image datatype, then use 'save/png instead of write/binary: repeat i 10 [save/png rejoin [%./ i %.png] load http://www.rebol.com/graphics/reb-logo.gif] Finally, to improve performance, it's important to cut down on repetitive work done in the loop. The code below should be dramatically fast compared to the examples above. It reads the file once from the URL, and then uses that saved file in the loop, instead of downloading from the web site each time (you can do this, unless you expect the file to change during each iteration of the loop): my-image: read/binary http://www.rebol.com/graphics/reb-logo.gif repeat i 10 [write/binary rejoin [%./ i %.gif] my-image]
posted by: Nick 9-Nov-2017/7:36:58-8:00
Amazing thank you!!!
posted by: tony 9-Nov-2017/13:21:09-8:00
"what's the difference between write and write to file ?" Not to pretend to read your mind, but the phrasing of your question reminds me of a difficulty I had with REBOL when I first encountered it. As explained above, the coding you were questioning was not "write to file" as in a command to write something into a file, but was the "write" function, followed by the "to" function to generate a file name out of a string. The "to" function requires, as its first argument, a data type declaration, in this case "file!" (note the exclamation mark). The "to" function then passed the generated file name back to the "write" function which was expecting a file name as its first argument. I came from a background of computer languages that were more command-oriented (do this, do that, do something else) and so when I first saw REBOL I thought it must follow that paradigm and so in my mind I kept trying to look for that pattern, and it was a bit confusing for a long time.
posted by: Steven White 13-Nov-2017/9:14:31-8:00
|