(Used {{Block Frame}}s to show how to join more than two values)
(59 intermediate revisions by 25 users not shown)
Line 1: Line 1:
{{Block
+
{{block
 
|name = Join ()()
 
|name = Join ()()
|image = File:Join_()().png
+
|image = join ()()
|imagewidth = 57px
 
 
|category = [[Operators Blocks|Operators]]
 
|category = [[Operators Blocks|Operators]]
 
|type = [[Reporter Block|Reporter]]
 
|type = [[Reporter Block|Reporter]]
 +
|defaults = "apple "; "banana"
 +
|argTypes = [[string]]; string
 +
|added = 1.4
 +
|sb = yes
 
}}
 
}}
The '''Join ()()''' [[Blocks|block]] is an [[Operators Blocks|Operators block]] and a [[Reporter Block|Reporter block]]. The block 'sticks' the two values together and reports the result - for example, if "hello" and "world" were put in the block, it would report "helloworld".
+
The '''Join ()()''' [[Blocks|block]] is an [[Operators Blocks|Operators block]] and a [[Reporter Block|Reporter block]]. The block concatenates, or "links" the two values together and reports the result {{-}} for example, if "hello" and "world" were put in the block, it would report "helloworld". To report "hello world", use either "hello " and "world" or "hello" and " world", with a space.
  
This block was introduced in [[Scratch 1.4]], along with other [[string]] blocks.
+
To join three or more values together, drag copies of this block into its inputs.
 +
{{block Frame|blocks=<scratchblocks>join (join (join [] []) []) (join [] [])</scratchblocks>|caption=Joining five strings}}
 +
{{block Frame|blocks=<scratchblocks>join [apple ] (join [banana ] [cherry])</scratchblocks>|caption=Joining three strings}}
  
The block's default values are "hello " and "world", respectfully.
 
 
==Example Uses==
 
==Example Uses==
If words, numbers, sentences - any two values - must be stuck together, this block easily does the job.
+
If words, numbers, sentences{{-}}any two values{{-}}must be concatenated together, this block easily does the job.
  
Some common uses for the '''Join ()()''' block:
+
Some common uses for the Join ()() block:
*Sticking together words and variables to create a sentence
+
* Joining words and variables to create a sentence
  
[[File:Hello (answer).gif]]
+
<scratchblocks>
 +
when flag clicked
 +
ask [What's your name?] and wait
 +
say (join (join (join (join [Hello ] (answer))[. My name is ]) (Person's name)) [.])
 +
</scratchblocks>
 +
 
 +
* Place a [[variable]] in a message (e.g. "You completed '''2''' loops")
 +
 
 +
<scratchblocks>say (join [The score is ] (join (score) [.]))</scratchblocks>
 +
 
 +
* An easy method of negating a positive number
 +
 
 +
<scratchblocks>set [variable v] to (join [-] (number))</scratchblocks>
 +
 
 +
{{note|This does not work on numbers that are already negative.}}
 +
 
 +
* Using hex inputs in pen colors
 +
 
 +
<scratchblocks>set pen color to (join [0x] (hex input))</scratchblocks>
 +
 
 +
===Offline decimal trick===
 +
In [[Scratch 1.4]], (this does not work online), numbers in variables and speech bubbles usually round to only one decimal place. In some [[project]]s, it is necessary to have more. Placing the calculation in one input of the Join ()() block and leaving the other input empty "magically" reports the number with more decimal places.
 +
* The original script:
 +
 
 +
<scratchblocks>
 +
set [number v] to ([sqrt v] of ((item (1 v) of [list v]) * (3)))
 +
</scratchblocks>
 +
 
 +
* Script with more decimals:
 +
 
 +
<scratchblocks>
 +
set [number v] to (join([sqrt v] of ((item (1 v) of [list v]) * (3))) [])
 +
</scratchblocks>
 +
 
 +
* To set the number of decimal places, use a script like this:
 +
 
 +
<scratchblocks>
 +
set [number v] to (join((round (([10^ v] of (decimal places)::operators) * ([sqrt v] of ((item (1 v) of [list v]) * (3))))) / ([10^ v] of (decimal places)::operators))[])
 +
</scratchblocks>
 +
 
 +
==Workaround==
 +
{{main|List of Block Workarounds}}
 +
This block can be replicated with the following code:
 +
 
 +
<scratchblocks>
 +
delete all of [final v]
 +
set [count v] to [0]
 +
repeat (length of (insert1))
 +
change [count v] by (1)
 +
add (letter (count) of (insert1)) to [final v]
 +
end
 +
set [count v] to [0]
 +
repeat (length of (insert2))
 +
change [count v] by (1)
 +
add (letter (count) of (insert2)) to [final v]
 +
end
 +
set [result v] to (final)
 +
</scratchblocks>
 +
 
 +
The variable "result" contains the result.
 +
This works because list reporters join all of the items in a list without spaces if they are all 1 character long, and so by taking just 1 letter from each string at a time and putting them into the list, the reporter has the same result as the join block in most cases.
 +
 
 +
==References==
 +
<references/>
  
*Place a [[variable]] in a message (eg. "You completed '''2''' loops")
 
*An easy method of making a positive number turn negative (placing "-" in the first input and the positive number in the second)
 
==Offline decimal trick==
 
Offline, numbers in variables and speech bubbles usually round to only two decimal places. In some [[Project|projects]], it is necessary to have more. Placing the calculation in one input of the '''Join ()()''' block and leaving the other input empty 'magically' reports the number with more decimal places.
 
*The original script:
 
[[File:No join.gif]]
 
*Script with more decimals:
 
[[File:Join Trick.gif]]
 
*To set the number of decimal places, use a script like this:
 
[[File: Set Decimal places.gif|550px]]
 
 
{{Operators Blocks}}
 
{{Operators Blocks}}
 
[[Category:Operators Blocks]]
 
[[Category:Operators Blocks]]
 +
[[de:Verbinde () () (Block)]]
 +
[[id:gabungkan ()() (balok)]]
 +
[[ja:() と () (ブロック)]]

Revision as of 17:55, 24 February 2021

Join ()()
join ()()
Category Operators
Type Reporter
Introduced in 1.4

The Join ()() block is an Operators block and a Reporter block. The block concatenates, or "links" the two values together and reports the result — for example, if "hello" and "world" were put in the block, it would report "helloworld". To report "hello world", use either "hello " and "world" or "hello" and " world", with a space.

To join three or more values together, drag copies of this block into its inputs.

join (join (join [] []) []) (join [] [])
Joining five strings
join [apple ] (join [banana ] [cherry])
Joining three strings

Example Uses

If words, numbers, sentences—any two values—must be concatenated together, this block easily does the job.

Some common uses for the Join ()() block:

  • Joining words and variables to create a sentence
when flag clicked
ask [What's your name?] and wait
say (join (join (join (join [Hello ] (answer))[. My name is ]) (Person's name)) [.])
  • Place a variable in a message (e.g. "You completed 2 loops")
say (join [The score is ] (join (score) [.]))
  • An easy method of negating a positive number
set [variable v] to (join [-] (number))
Note Note: This does not work on numbers that are already negative.
  • Using hex inputs in pen colors
set pen color to (join [0x] (hex input))

Offline decimal trick

In Scratch 1.4, (this does not work online), numbers in variables and speech bubbles usually round to only one decimal place. In some projects, it is necessary to have more. Placing the calculation in one input of the Join ()() block and leaving the other input empty "magically" reports the number with more decimal places.

  • The original script:
set [number v] to ([sqrt v] of ((item (1 v) of [list v]) * (3)))
  • Script with more decimals:
set [number v] to (join([sqrt v] of ((item (1 v) of [list v]) * (3))) [])
  • To set the number of decimal places, use a script like this:
set [number v] to (join((round (([10^ v] of (decimal places)::operators) * ([sqrt v] of ((item (1 v) of [list v]) * (3))))) / ([10^ v] of (decimal places)::operators))[])

Workaround

Main article: List of Block Workarounds

This block can be replicated with the following code:

delete all of [final v]
set [count v] to [0]
repeat (length of (insert1))
change [count v] by (1)
add (letter (count) of (insert1)) to [final v]
end
set [count v] to [0]
repeat (length of (insert2))
change [count v] by (1)
add (letter (count) of (insert2)) to [final v]
end
set [result v] to (final)

The variable "result" contains the result. This works because list reporters join all of the items in a list without spaces if they are all 1 character long, and so by taking just 1 letter from each string at a time and putting them into the list, the reporter has the same result as the join block in most cases.

References


Cookies help us deliver our services. By using our services, you agree to our use of cookies.