Long literals, with continuations: Difference between revisions
m (→{{header|J}}) |
m (→{{header|Wren}}: Changed to use a 'raw' string.) |
||
(48 intermediate revisions by 25 users not shown) | |||
Line 66: | Line 66: | ||
Show all output here, on this page. |
Show all output here, on this page. |
||
{{Template:Strings}} |
|||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">V revdate = ‘2021-11-14’ |
|||
V elements = |
|||
‘hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine ’"" |
|||
‘neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon ’"" |
|||
‘potassium calcium scandium titanium vanadium chromium manganese iron ’"" |
|||
‘cobalt nickel copper zinc gallium germanium arsenic selenium bromine ’"" |
|||
‘krypton rubidium strontium yttrium zirconium niobium molybdenum ’"" |
|||
‘technetium ruthenium rhodium palladium silver cadmium indium tin ’"" |
|||
‘antimony tellurium iodine xenon cesium barium lanthanum cerium ’"" |
|||
‘praseodymium neodymium promethium samarium europium gadolinium terbium ’"" |
|||
‘dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum ’"" |
|||
‘tungsten rhenium osmium iridium platinum gold mercury thallium lead ’"" |
|||
‘bismuth polonium astatine radon francium radium actinium thorium ’"" |
|||
‘protactinium uranium neptunium plutonium americium curium berkelium ’"" |
|||
‘californium einsteinium fermium mendelevium nobelium lawrencium ’"" |
|||
‘rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium ’"" |
|||
‘roentgenium copernicium nihonium flerovium moscovium livermorium ’"" |
|||
‘tennessine oganesson’ |
|||
V items = elements.split(‘ ’) |
|||
print(‘Last revision date: ’revdate) |
|||
print(‘Number of elements: ’items.len) |
|||
print(‘Last element : ’items.last)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Last revision date: 2021-11-14 |
|||
Number of elements: 118 |
|||
Last element : oganesson |
|||
</pre> |
|||
=={{header|6502 Assembly}}== |
|||
There is no need for continuation codes, as the language interprets a contiguous data block as a single entity, whether you intended it to be or not. If you had two consecutive strings embedded in the code and you forgot the null terminator on the first one, trying to print just the first string would print both. For example: |
|||
<syntaxhighlight lang="6502asm">HelloString: |
|||
db "Hello World" ;no null terminator |
|||
GoodbyeString: |
|||
db "Goodbye World!",0 |
|||
PrintString HelloString ;unimplemented macro.</syntaxhighlight> |
|||
The output would be as follows, assuming the <code>PrintString</code> routine uses a null terminator to know when a string ends: |
|||
<pre> |
|||
Hello WorldGoodbye World! |
|||
</pre> |
|||
In addition, it makes no difference to the assembler whether each string is on its own line, or each is one after the other on the same line. It's easier to put each on its own line since you can't have a label mid-line. |
|||
Since each string is of variable length, it is much easier to make a lookup table of the elements ordered by atomic number, with each element in the lookup table being a pointer to the actual string consisting of that element's name. |
|||
<syntaxhighlight lang="6502asm">ElementNull: |
|||
dw nullString |
|||
Elements: |
|||
; this is typed in a compact manner to save on typing, however putting each on its own line with a |
|||
; "dw" directive in front will produce the same result. A comment with the element number on each line will aid in |
|||
; adding new elements to the list. |
|||
dw hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine |
|||
dw neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon |
|||
dw potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron |
|||
dw cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine |
|||
dw krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum |
|||
dw technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin |
|||
dw antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium |
|||
dw praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium |
|||
dw dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum |
|||
dw tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead |
|||
dw bismuth,polonium,astatine,radon,francium,radium,actinium,thorium |
|||
dw protactinium,uranium,neptunium,plutonium,americium,curium,berkelium |
|||
dw californium,einsteinium,fermium,mendelevium,nobelium,lawrencium |
|||
dw rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium |
|||
dw roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium |
|||
dw tennessine,oganesson |
|||
Elements_End: |
|||
nullString: |
|||
db 0 |
|||
hydrogen: |
|||
db "hydrogen",0 |
|||
helium: |
|||
db "helium",0 |
|||
lithium: |
|||
db "lithium",0 |
|||
;etc. |
|||
RevisionDate: |
|||
db "2021-Sep-20th",0 |
|||
Finally: |
|||
db "elements, the last is",0 |
|||
ElementCount equ (Elements_End-Elements)/2 |
|||
; a constant value that cannot change at runtime. |
|||
; This counts the number of bytes between the two labels, and automatically adjusts when the size of the list changes. |
|||
; The division by 2 gets the actual element count since each address is 2 bytes long.</syntaxhighlight> |
|||
The required output can be obtained like so: |
|||
<syntaxhighlight lang="6502asm">LDA #<RevisionDate ;get the low byte of the address |
|||
STA z_L ;store it in z_L, a zero page memory address |
|||
LDA #>RevisionDate ;get the high byte |
|||
STA z_H ;store it in z_H, the zero page memory address directly after z_L |
|||
JSR PrintString ;unimplemented printing routine |
|||
JSR NewLine |
|||
LDA ElementCount |
|||
JSR ConvertHex2BinDec ;converts a hexadecimal value to a trio of BCD digits |
|||
JSR PrintBCD ;unimplemented printing routine for numeric values |
|||
LDA #$20 ;ASCII for spacebar |
|||
JSR PrintChar |
|||
LDA #<Finally |
|||
STA z_L |
|||
LDA #>Finally |
|||
STA z_H |
|||
JSR PrintString |
|||
LDA #$20 ;ASCII for spacebar |
|||
JSR PrintChar |
|||
LDA ElementCount |
|||
ASL A ;multiply by 2, we are indexing into a table of words. |
|||
TAX ;use as an offset into the lookup table. |
|||
LDA ElementNull,x ;ElementCount doesn't account for zero-indexing so we'll need to load one word behind. |
|||
STA z_L |
|||
LDA ElementNull+1,x |
|||
STA z_H |
|||
JSR PrintString</syntaxhighlight> |
|||
This routine can be expanded as new elements are discovered, but once the 128th element is discovered it will need to be reprogrammed since you can't offset more than x=255. This is actually a very simple fix, and can be accomplished by splitting the table as such: |
|||
<syntaxhighlight lang="6502asm">Elements_Lo: |
|||
db <hydrogen,<helium,<lithium,<beryllium,<boron,<carbon,<nitrogen,<oxygen,<fluorine,... |
|||
Elements_Hi: |
|||
db >hydrogen,>helium,>lithium,>beryllium,>boron,>carbon,>nitrogen,>oxygen,>fluorine,...</syntaxhighlight> |
|||
Note that < and > are unary operators that mean "the low byte of" and "the high byte of", respectively. By storing the halves of each pointer in two separate tables, sharing a common index, we can index up to 256 elements rather than 128, without increasing the total data size of the tables. |
|||
{{out}} |
|||
<pre> |
|||
2021-Sep-20th |
|||
118 elements, the last is oganesson |
|||
</pre> |
|||
=={{header|68000 Assembly}}== |
|||
{{trans|6502 Assembly}} |
|||
There is no need for continuation codes, as the language interprets a contiguous data block as a single entity, whether you intended it to be or not. If you had two consecutive strings embedded in the code and you forgot the null terminator on the first one, trying to print just the first string would print both. For example: |
|||
<syntaxhighlight lang="68000devpac">HelloString: |
|||
DC.B "Hello World" ;no null terminator |
|||
GoodbyeString: |
|||
DC.B "Goodbye World!",0 |
|||
EVEN |
|||
PrintString HelloString ;unimplemented macro.</syntaxhighlight> |
|||
The output would be as follows, assuming the <code>PrintString</code> routine uses a null terminator to know when a string ends: |
|||
<pre> |
|||
Hello WorldGoodbye World! |
|||
</pre> |
|||
In addition, it makes no difference to the assembler whether each string is on its own line, or each is one after the other on the same line. It's easier to put each on its own line since you can't have a label mid-line. |
|||
Since each string is of variable length, it is much easier to make a lookup table of the elements ordered by atomic number, with each element in the lookup table being a pointer to the actual string consisting of that element's name. |
|||
<syntaxhighlight lang="68000devpac">NullElement: |
|||
DC.L nullString |
|||
Elements: |
|||
; this is typed in a compact manner to save on typing, however putting each on its own line with a |
|||
; "DC.L" directive in front will produce the same result. A comment with the element number on each line will aid in |
|||
; adding new elements to the list. |
|||
DC.L hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine |
|||
DC.L neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon |
|||
DC.L potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron |
|||
DC.L cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine |
|||
DC.L krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum |
|||
DC.L technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin |
|||
DC.L antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium |
|||
DC.L praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium |
|||
DC.L dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum |
|||
DC.L tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead |
|||
DC.L bismuth,polonium,astatine,radon,francium,radium,actinium,thorium |
|||
DC.L protactinium,uranium,neptunium,plutonium,americium,curium,berkelium |
|||
DC.L californium,einsteinium,fermium,mendelevium,nobelium,lawrencium |
|||
DC.L rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium |
|||
DC.L roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium |
|||
DC.L tennessine,oganesson |
|||
Elements_End: |
|||
nullString: |
|||
DC.B 0 |
|||
EVEN |
|||
hydrogen: |
|||
DC.B "hydrogen",0 |
|||
EVEN |
|||
helium: |
|||
DC.B "helium",0 |
|||
EVEN |
|||
lithium: |
|||
DC.B "lithium",0 |
|||
EVEN |
|||
;etc. |
|||
RevisionDate: |
|||
DC.B "2021-Sep-20th",0 |
|||
EVEN |
|||
Finally: |
|||
DC.B "elements, the last is",0 |
|||
EVEN |
|||
ElementCount equ (Elements_End-Elements)/4 |
|||
; a constant value that cannot change at runtime. |
|||
; This counts the number of bytes between the two labels, and automatically adjusts when the size of the list changes. |
|||
; The division by 4 gets the actual element count since each address is 4 bytes long.</syntaxhighlight> |
|||
The required output can be obtained like so: |
|||
<syntaxhighlight lang="68000devpac">LEA RevisionDate,A3 ; the printing routine uses A3 as input |
|||
JSR PrintString ; unimplemented printing routine |
|||
JSR NewLine ; unimplemented new line routine |
|||
MOVE.B ElementCount,D0 |
|||
JSR ConvertHex2BinDec ; converts a hexadecimal value to a trio of BCD digits |
|||
JSR PrintBCD ; unimplemented printing routine for numeric values |
|||
MOVE.B #' ',D0 ; ASCII for spacebar |
|||
JSR PrintChar |
|||
LEA Finally,A3 |
|||
JSR PrintString |
|||
MOVE.B #' ',D0 ; ASCII for spacebar |
|||
JSR PrintChar |
|||
MOVE.W ElementCount,D1 |
|||
LSL.W #2,D1 ; multiply by 4, we are indexing into a table of longs |
|||
LEA NullElement,A2 |
|||
; load the base of the lookup table into A2. This is the table's "true base" |
|||
; since the "ElementCount" constant doesn't account for zero-indexing |
|||
MOVEA.L (A2,D1),A3 |
|||
;dereference the pointer, offsetting by D1. This retrieves the address of the desired element in the list. |
|||
JSR PrintString</syntaxhighlight> |
|||
This routine can easily be expanded for more elements. With a maximum offset of 16,383, it's unlikely that the table will get too large to properly index anytime soon. |
|||
{{out}} |
|||
<pre> |
|||
2021-Sep-20th |
|||
118 elements, the last is oganesson |
|||
</pre> |
|||
=={{header|Arturo}}== |
|||
===Using concatenations=== |
|||
<syntaxhighlight lang="rebol">revDate: "2021-02-05" |
|||
elementString: |
|||
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine " ++ |
|||
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon " ++ |
|||
"potassium calcium scandium titanium vanadium chromium manganese iron " ++ |
|||
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine " ++ |
|||
"krypton rubidium strontium yttrium zirconium niobium molybdenum " ++ |
|||
"technetium ruthenium rhodium palladium silver cadmium indium tin " ++ |
|||
"antimony tellurium iodine xenon cesium barium lanthanum cerium " ++ |
|||
"praseodymium neodymium promethium samarium europium gadolinium terbium " ++ |
|||
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " ++ |
|||
"tungsten rhenium osmium iridium platinum gold mercury thallium lead " ++ |
|||
"bismuth polonium astatine radon francium radium actinium thorium " ++ |
|||
"protactinium uranium neptunium plutonium americium curium berkelium " ++ |
|||
"californium einsteinium fermium mendelevium nobelium lawrencium " ++ |
|||
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium " ++ |
|||
"roentgenium copernicium nihonium flerovium moscovium livermorium " ++ |
|||
"tennessine oganesson" |
|||
elements: split.words elementString |
|||
print ["Last revision date:" revDate] |
|||
print ["Number of elements:" size elements] |
|||
print ["Last element in list:" last elements]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision date: 2021-02-05 |
|||
Number of elements: 118 |
|||
Last element in list: oganesson</pre> |
|||
===Using string blocks=== |
|||
<syntaxhighlight lang="rebol">revDate: "2021-02-05" |
|||
elementString: { |
|||
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine |
|||
neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon |
|||
potassium calcium scandium titanium vanadium chromium manganese iron |
|||
cobalt nickel copper zinc gallium germanium arsenic selenium bromine |
|||
krypton rubidium strontium yttrium zirconium niobium molybdenum |
|||
technetium ruthenium rhodium palladium silver cadmium indium tin |
|||
antimony tellurium iodine xenon cesium barium lanthanum cerium |
|||
praseodymium neodymium promethium samarium europium gadolinium terbium |
|||
dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum |
|||
tungsten rhenium osmium iridium platinum gold mercury thallium lead |
|||
bismuth polonium astatine radon francium radium actinium thorium |
|||
protactinium uranium neptunium plutonium americium curium berkelium |
|||
californium einsteinium fermium mendelevium nobelium lawrencium |
|||
rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium |
|||
roentgenium copernicium nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
} |
|||
elements: split.words elementString |
|||
print ["Last revision date:" revDate] |
|||
print ["Number of elements:" size elements] |
|||
print ["Last element in list:" last elements]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision date: 2021-02-05 |
|||
Number of elements: 118 |
|||
Last element in list: oganesson</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f LONG_LITERALS_WITH_CONTINUATIONS.AWK |
# syntax: GAWK -f LONG_LITERALS_WITH_CONTINUATIONS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 166: | Line 493: | ||
revised = "30JUN2020" |
revised = "30JUN2020" |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 179: | Line 506: | ||
first & last 30 characters: hydrogen helium lithium beryll & ivermorium tennessine oganesson |
first & last 30 characters: hydrogen helium lithium beryll & ivermorium tennessine oganesson |
||
</pre> |
</pre> |
||
=={{header|BASIC256}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="basic256"> |
|||
ultimaRevision$ = "2021-11-12" |
|||
arraybase 1 |
|||
dim elemento$ = {"hydrogen", "helium", "lithium", "beryllium", "boron", "carbon", "nitrogen", "oxygen", "fluorine", "neon", "sodium", "magnesium", "aluminum", "silicon", "phosphorous", "sulfur", "chlorine", "argon", "potassium", "calcium", "scandium", "titanium", "vanadium", "chromium", "manganese", "iron", "cobalt", "nickel", "copper", "zinc", "gallium", "germanium", "arsenic", "selenium", "bromine", "krypton", "rubidium", "strontium", "yttrium", "zirconium", "niobium", "molybdenum", "technetium", "ruthenium", "rhodium", "palladium", "silver", "cadmium", "indium", "tin", "antimony", "tellurium", "iodine", "xenon", "cesium", "barium", "lanthanum", "cerium", "praseodymium", "neodymium", "promethium", "samarium", "europium", "gadolinium", "terbium", "dysprosium", "holmium", "erbium", "thulium", "ytterbium", "lutetium", "hafnium", "tantalum", "tungsten", "rhenium", "osmium", "iridium", "platinum", "gold", "mercury", "thallium", "lead", "bismuth", "polonium", "astatine", "radon", "francium", "radium", "actinium", "thorium", "protactinium", "uranium", "neptunium", "plutonium", "americium", "curium", "berkelium", "californium", "einsteinium", "fermium", "mendelevium", "nobelium", "lawrencium", "rutherfordium", "dubnium", "seaborgium", "bohrium", "hassium", "meitnerium", "darmstadtium", "roentgenium", "copernicium", "nihonium", "flerovium", "moscovium", "livermorium", "tennessine", "oganesson"} |
|||
print "Last updated : "; ultimaRevision$ |
|||
print "Number of elements : "; elemento$[?] |
|||
print "Last element : "; elemento$[elemento$[?]] |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <chrono> |
|||
#include <format> |
|||
#include <iostream> |
|||
#include <regex> |
|||
#include <sstream> |
|||
#include <string> |
|||
const std::string ELEMENTS = R"( |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
)"; |
|||
const std::string UNNAMED_ELEMENTS = R"( |
|||
ununennium unquadnilium triunhexium penthextrium |
|||
penthexpentium septhexunium octenntrium ennennbium |
|||
)"; |
|||
std::vector<std::string> rawstring_to_vector(const std::string& text, const char& delimiter) { |
|||
std::regex regx("\\s+"); |
|||
std::string delimit(1, delimiter); |
|||
std::string elements = std::regex_replace(text, regx, delimit); |
|||
elements = elements.substr(1, elements.size() - 2); |
|||
std::vector<std::string> result; |
|||
std::stringstream stream(elements); |
|||
std::string item; |
|||
while ( getline(stream, item, delimiter) ) { |
|||
result.push_back (item); |
|||
} |
|||
return result; |
|||
} |
|||
int main() { |
|||
std::vector<std::string> elements = rawstring_to_vector(ELEMENTS, ' '); |
|||
std::vector<std::string> unnamed = rawstring_to_vector(UNNAMED_ELEMENTS, ' '); |
|||
elements.erase(std::remove_if(elements.begin(), elements.end(), |
|||
[unnamed](std::string text){ return std::find(unnamed.begin(), unnamed.end(), text) != unnamed.end(); }), |
|||
elements.end()); |
|||
const std::string zone = "Asia/Shanghai"; |
|||
const std::chrono::zoned_time zoned_time { zone, std::chrono::system_clock::now() }; |
|||
std::cout << "Last revision Date: " << std::format("{:%Y-%m-%d Time %H:%M}", zoned_time) |
|||
<< " " << zone << std::endl; |
|||
std::cout << "Number of elements: " << elements.size() << std::endl; |
|||
std::cout << "Last element : " << elements[elements.size() - 1] << std::endl; |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Last revision Date: 2023-08-04 Time 00:18 Asia/Shanghai |
|||
Number of elements: 118 |
|||
Last element : oganesson |
|||
</pre> |
|||
=={{header|Crystal}}== |
|||
Crystal's <code>%w</code> literals create a whitespace-delimited array of strings at compile time |
|||
<syntaxhighlight lang="ruby">require "time" |
|||
last_revision = Time.utc year: 2021, month: 2, day: 25 |
|||
# the `%w()` literal creates an array from a whitespace-delimited string literal |
|||
# it's equivalent to %(string literal).split |
|||
# https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html |
|||
element_list : Array(String) = %w( |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson) |
|||
puts last_revision.to_s "last revised %B %e, %Y" |
|||
puts "number of elements: #{element_list.size}" |
|||
puts "highest element: #{element_list.last}"</syntaxhighlight> |
|||
=={{header|Delphi}}== |
|||
{{libheader| System.SysUtils}} |
|||
{{libheader| System.StrUtils}} |
|||
{{Trans|Free Pascal}} |
|||
<syntaxhighlight lang="delphi"> |
|||
program Long_literals_with_continuations; |
|||
{$APPTYPE CONSOLE} |
|||
uses |
|||
System.SysUtils, |
|||
System.StrUtils; |
|||
// Copy and past of Free_Pascal version |
|||
const |
|||
StdWordDelims: array[0..16] of char = (#0, ' ', ',', '.', ';', '/', '\', ':', |
|||
'''', '"', '`', '(', ')', '[', ']', '{', '}'); |
|||
revisionNotice = 'Last update: %0:s'; |
|||
elementString = 'hydrogen helium lithium beryllium boron carbon nitrogen oxy' |
|||
+ 'gen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chl' |
|||
+ 'orine argon potassium calcium scandium titanium vanadium chromium manganes' |
|||
+ 'e iron cobalt nickel copper zinc gallium germanium arsenic selenium bromin' |
|||
+ 'e krypton rubidium strontium yttrium zirconium niobium molybdenum techneti' |
|||
+ 'um ruthenium rhodium palladium silver cadmium indium tin antimony telluriu' |
|||
+ 'm iodine xenon cesium barium lanthanum cerium praseodymium neodymium prome' |
|||
+ 'thium samarium europium gadolinium terbium dysprosium holmium erbium thuli' |
|||
+ 'um ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium pla' |
|||
+ 'tinum gold mercury thallium lead bismuth polonium astatine radon francium ' |
|||
+ 'radium actinium thorium protactinium uranium neptunium plutonium americium' |
|||
+ ' curium berkelium californium einsteinium fermium mendelevium nobelium law' |
|||
+ 'rencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmst' |
|||
+ 'adtium roentgenium copernicium nihonium flerovium moscovium livermorium te' |
|||
+ 'nnessine oganesson'; |
|||
elementRevision = '2020-11-11'; |
|||
begin |
|||
var words := elementString.Split(StdWordDelims); |
|||
writeLn(format(revisionNotice, [elementRevision])); |
|||
writeln(length(words)); |
|||
writeln(words[high(words)]); |
|||
readln; |
|||
end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last update: 2020-11-11 |
|||
118 |
|||
oganesson</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
The <code>qw</code> vocabulary provides Perl-ish syntax for arrays of strings. For instance, the literal <pre>qw{ a bc d }</pre> expands to <pre>{ "a" "bc" "d" }</pre> during parse time. This is convenient to use when the strings that are stored contain no whitespace. |
The <code>qw</code> vocabulary provides Perl-ish syntax for arrays of strings. For instance, the literal <pre>qw{ a bc d }</pre> expands to <pre>{ "a" "bc" "d" }</pre> during parse time. This is convenient to use when the strings that are stored contain no whitespace. |
||
Line 185: | Line 711: | ||
The convention in Factor is to limit lines to 64 characters wide if possible. This constraint is sometimes waived for large literals, but it was easy enough to accommodate here. |
The convention in Factor is to limit lines to 64 characters wide if possible. This constraint is sometimes waived for large literals, but it was easy enough to accommodate here. |
||
{{works with|Factor|0.99 2020-03-02}} |
{{works with|Factor|0.99 2020-03-02}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting kernel qw sequences ; |
||
qw{ |
qw{ |
||
Line 224: | Line 750: | ||
"Last revision: %s\n" printf |
"Last revision: %s\n" printf |
||
[ length ] [ last ] bi |
[ length ] [ last ] bi |
||
"Number of elements: %d\nLast element: %s\n" printf</ |
"Number of elements: %d\nLast element: %s\n" printf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 231: | Line 757: | ||
Last element: oganesson |
Last element: oganesson |
||
</pre> |
</pre> |
||
=={{header|Free Pascal}}== |
|||
The <tt>{$longStrings on}</tt> compiler directive enables the ANSI string data type. |
|||
ANSI strings are pointers to sequences of characters. |
|||
The data type includes a reference count and a (4-Byte long) <tt>length</tt> field. |
|||
<syntaxhighlight lang="pascal"> |
|||
program longStringLiteralDemo(output); |
|||
{$mode objFPC} |
|||
{$longStrings on} |
|||
uses |
|||
// for `format` |
|||
sysUtils, |
|||
// for `wordCount` and `extractWord` |
|||
strUtils; |
|||
const |
|||
elementString = 'hydrogen helium lithium beryllium boron carbon nitrogen oxy' + |
|||
'gen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chl' + |
|||
'orine argon potassium calcium scandium titanium vanadium chromium manganes' + |
|||
'e iron cobalt nickel copper zinc gallium germanium arsenic selenium bromin' + |
|||
'e krypton rubidium strontium yttrium zirconium niobium molybdenum techneti' + |
|||
'um ruthenium rhodium palladium silver cadmium indium tin antimony telluriu' + |
|||
'm iodine xenon cesium barium lanthanum cerium praseodymium neodymium prome' + |
|||
'thium samarium europium gadolinium terbium dysprosium holmium erbium thuli' + |
|||
'um ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium pla' + |
|||
'tinum gold mercury thallium lead bismuth polonium astatine radon francium ' + |
|||
'radium actinium thorium protactinium uranium neptunium plutonium americium' + |
|||
' curium berkelium californium einsteinium fermium mendelevium nobelium law' + |
|||
'rencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmst' + |
|||
'adtium roentgenium copernicium nihonium flerovium moscovium livermorium te' + |
|||
'nnessine oganesson'; |
|||
elementRevision = '2020‑11‑11'; |
|||
resourcestring |
|||
revisionNotice = 'Last update: %0:s'; |
|||
begin |
|||
writeLn(format(revisionNotice, [elementRevision])); |
|||
writeLn(wordCount(elementString, stdWordDelims)); |
|||
writeLn(extractWord(wordCount(elementString, stdWordDelims), |
|||
elementString, stdWordDelims)); |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Last update: 2020‑11‑11 |
|||
118 |
|||
oganesson |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
Dim As String ultimaRevision = "2021-11-12" |
|||
Dim As String elemento(0 to ...) => { _ |
|||
"hydrogen", "helium", "lithium", "beryllium", "boron", "carbon", "nitrogen", _ |
|||
"oxygen", "fluorine", "neon", "sodium", "magnesium", "aluminum", "silicon", _ |
|||
"phosphorous", "sulfur", "chlorine", "argon", "potassium", "calcium", _ |
|||
"scandium", "titanium", "vanadium", "chromium", "manganese", "iron", "cobalt", _ |
|||
"nickel", "copper", "zinc", "gallium", "germanium", "arsenic", "selenium", _ |
|||
"bromine", "krypton", "rubidium", "strontium", "yttrium", "zirconium", _ |
|||
"niobium", "molybdenum", "technetium", "ruthenium", "rhodium", "palladium", _ |
|||
"silver", "cadmium", "indium", "tin", "antimony", "tellurium", "iodine", _ |
|||
"xenon", "cesium", "barium", "lanthanum", "cerium", "praseodymium", _ |
|||
"neodymium", "promethium", "samarium", "europium", "gadolinium", "terbium", _ |
|||
"dysprosium", "holmium", "erbium", "thulium", "ytterbium", "lutetium", _ |
|||
"hafnium", "tantalum", "tungsten", "rhenium", "osmium", "iridium", "platinum", _ |
|||
"gold", "mercury", "thallium", "lead", "bismuth", "polonium", "astatine", _ |
|||
"radon", "francium", "radium", "actinium", "thorium", "protactinium", _ |
|||
"uranium", "neptunium", "plutonium", "americium", "curium", "berkelium", _ |
|||
"californium", "einsteinium", "fermium", "mendelevium", "nobelium", _ |
|||
"lawrencium", "rutherfordium", "dubnium", "seaborgium", "bohrium", "hassium", _ |
|||
"meitnerium", "darmstadtium", "roentgenium", "copernicium", "nihonium", _ |
|||
"flerovium", "moscovium", "livermorium", "tennessine", "oganesson"} |
|||
Print "Last updated : "; ultimaRevision |
|||
Print "Number of elements : "; Ubound(elemento) - Lbound(elemento) + 1 |
|||
Print "Last element : "; elemento(Ubound(elemento)) |
|||
Sleep |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Last updated : 2021-11-12 |
|||
Number of elements : 118 |
|||
Last element : oganesson |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 291: | Line 906: | ||
lix := strings.LastIndex(elements2, " ") // get index of last space |
lix := strings.LastIndex(elements2, " ") // get index of last space |
||
fmt.Println("Last element : ", elements2[lix+1:]) |
fmt.Println("Last element : ", elements2[lix+1:]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 299: | Line 914: | ||
Last element : oganesson |
Last element : oganesson |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<syntaxhighlight lang="haskell">elements = words "hydrogen \ |
|||
\ fluorine neon sodium magnesium \ |
|||
\ aluminum silicon phosphorous sulfur \ |
|||
\ chlorine argon potassium calcium \ |
|||
\ scandium titanium vanadium chromium \ |
|||
\ manganese iron cobalt nickel \ |
|||
\ copper zinc gallium germanium \ |
|||
\ arsenic selenium bromine krypton \ |
|||
\ rubidium strontium yttrium zirconium \ |
|||
\ niobium molybdenum technetium ruthenium \ |
|||
\ rhodium palladium silver cadmium \ |
|||
\ indium tin antimony tellurium \ |
|||
\ iodine xenon cesium barium \ |
|||
\ lanthanum cerium praseodymium neodymium \ |
|||
\ promethium samarium europium gadolinium \ |
|||
\ terbium dysprosium holmium erbium \ |
|||
\ thulium ytterbium lutetium hafnium \ |
|||
\ tantalum tungsten rhenium osmium \ |
|||
\ iridium platinum gold mercury \ |
|||
\ thallium lead bismuth polonium \ |
|||
\ astatine radon francium radium \ |
|||
\ actinium thorium protactinium uranium \ |
|||
\ neptunium plutonium americium curium \ |
|||
\ berkelium californium einsteinium fermium \ |
|||
\ mendelevium nobelium lawrencium rutherfordium \ |
|||
\ dubnium seaborgium bohrium hassium \ |
|||
\ meitnerium darmstadtium roentgenium copernicium \ |
|||
\ nihonium flerovium moscovium livermorium \ |
|||
\ tennessine oganesson"</syntaxhighlight> |
|||
<pre>*Main> length elements |
|||
111 |
|||
*Main> take 5 elements |
|||
["hydrogen","fluorine","neon","sodium","magnesium"] |
|||
*Main> filter (('a' ==) . head) elements |
|||
["aluminum","argon","arsenic","antimony","astatine","actinium","americium"]</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 357: | Line 1,014: | ||
│hydrogen helium lithium beryll│vermorium tennessine oganesson│ |
│hydrogen helium lithium beryll│vermorium tennessine oganesson│ |
||
└──────────────────────────────┴──────────────────────────────┘</pre> |
└──────────────────────────────┴──────────────────────────────┘</pre> |
||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.time.ZoneId; |
|||
import java.time.ZonedDateTime; |
|||
import java.time.format.DateTimeFormatter; |
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
public final class LongLiteralsWithContinuations { |
|||
public static void main(String[] aArgs) { |
|||
ZoneId zoneID = ZoneId.of("Asia/Shanghai"); |
|||
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneID); |
|||
String dateTime = zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a")); |
|||
List<String> elements = splitToList(ELEMENTS).stream(). |
|||
filter( s -> ! splitToList(UNNAMED_ELEMENTS).contains(s) ).toList(); |
|||
System.out.println("Last revision Date: " + dateTime + " " + zoneID); |
|||
System.out.println("Number of elements: " + elements.size()); |
|||
System.out.println("Last element : " + elements.get(elements.size() - 1)); |
|||
} |
|||
private static List<String> splitToList(String aText) { |
|||
String excessWhiteSpaceRemoved = aText.trim().replaceAll("\\s+", " "); |
|||
return Arrays.stream(excessWhiteSpaceRemoved.split(" ")).toList(); |
|||
} |
|||
private static final String ELEMENTS = """ |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
"""; |
|||
private static final String UNNAMED_ELEMENTS = """ |
|||
ununennium unquadnilium triunhexium penthextrium |
|||
penthexpentium septhexunium octenntrium ennennbium |
|||
"""; |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Last revision Date: 2023-08-03 19:19:48 pm Asia/Shanghai |
|||
Number of elements: 118 |
|||
Last element : oganesson |
|||
</pre> |
|||
=={{header|jq}}== |
|||
<syntaxhighlight lang="jq"># FOR FUTURE EDITORS: |
|||
# To add chemical elements, modify the CHEMICAL_ELEMENTS function, |
|||
# ensuring that the date is updated properly and that there is at least one |
|||
# space between the element names after concatenation of the strings. |
|||
# Do not include any of the "unnamed" chemical element names such as ununennium. |
|||
def CHEMICAL_ELEMENTS: |
|||
{date: "Wed Jun 23 00:00:00 EDT 2021", |
|||
elements: ( |
|||
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon " |
|||
+ "sodium magnesium aluminum silicon phosphorus sulfur chlorine argon potassium " |
|||
+ "calcium scandium titanium vanadium chromium manganese iron cobalt nickel copper " |
|||
+ "zinc gallium germanium arsenic selenium bromine krypton rubidium strontium " |
|||
+ "yttrium zirconium niobium molybdenum technetium ruthenium rhodium palladium " |
|||
+ "silver cadmium indium tin antimony tellurium iodine xenon cesium barium " |
|||
+ "lanthanum cerium praseodymium neodymium promethium samarium europium gadolinium " |
|||
+ "terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " |
|||
+ "tungsten rhenium osmium iridium platinum gold mercury thallium lead bismuth " |
|||
+ "polonium astatine radon francium radium actinium thorium protactinium uranium " |
|||
+ "neptunium plutonium americium curium berkelium californium einsteinium fermium " |
|||
+ "mendelevium nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium " |
|||
+ "meitnerium darmstadtium roentgenium copernicium nihonium flerovium moscovium " |
|||
+ "livermorium tennessine oganesson" |
|||
) } |
|||
; |
|||
def chemical_elements_array: |
|||
CHEMICAL_ELEMENTS.elements |
|||
# remove leading and trailing whitespace |
|||
| sub("^ *";"") | sub(" *$";"") |
|||
# return a list after splitting using whitespace between words as a separator |
|||
| [splits("[ \t]+")] ; |
|||
def report: |
|||
chemical_elements_array as $a |
|||
| "List last revised: \(CHEMICAL_ELEMENTS.date)", |
|||
"Length of element list: \($a|length)", |
|||
"Last element in list: \($a[-1])"; |
|||
report</syntaxhighlight> |
|||
Invocation: jq -nrf program.jq |
|||
{{out}} |
|||
<pre> |
|||
List last revised: Wed Jun 23 00:00:00 EDT 2021 |
|||
Length of element list: 118 |
|||
Last element in list: oganesson</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
The task does not as of the current revision mention lower versus upper case, but the below is corrected per a request anyway. |
The task does not as of the current revision mention lower versus upper case, but the below is corrected per a request anyway. |
||
The task does ask to comment on which column code may start. The start column for code does not matter to Julia, or to most modern computer language compilers, other than in some cases (not string data) Python. |
The task does ask to comment on which column code may start. The start column for code does not matter to Julia, or to most modern computer language compilers, other than in some cases (not string data) Python. |
||
< |
<syntaxhighlight lang="julia">using Dates |
||
# FOR FUTURE EDITORS: |
# FOR FUTURE EDITORS: |
||
Line 414: | Line 1,196: | ||
report() |
report() |
||
</ |
</syntaxhighlight> {{out}} |
||
<pre> |
<pre> |
||
File last revised (formatted as dateTtime): 2020-03-24T02:48:55.421 GMT |
File last revised (formatted as dateTtime): 2020-03-24T02:48:55.421 GMT |
||
Line 421: | Line 1,203: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Kotlin}}== |
||
Kotlin has raw string literals with <code>"""</code>. It does not (yet) have a native date/time library, so we use Java’s here. |
|||
Back-ticks and triple-quotes permit multi-line strings. We first replace all/any cr/lf/tab characters with spaces, then split (by default on a single space), omitting empty elements. You could use spaced_elements = join(elements) to join them back up into a space-separated single string, if that's really what you want, and you could then, like Go, use rfind(' ',spaced_elements) to re-extract the last one. |
|||
You could also, like Julia, use get_file_date(command_line()[2]) instead of the hand-written last_updated constant. Phix code is free-format, indent things however you like, there is no specific maximum line length. |
|||
<syntaxhighlight lang="kotlin">import java.time.Instant |
|||
<lang Phix>constant last_updated = "March 24th, 2020", |
|||
elements_text = ` |
|||
const val elementsChunk = """ |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
""" |
|||
const val unamedElementsChunk = """ |
|||
ununennium unquadnilium triunhexium penthextrium |
|||
penthexpentium septhexunium octenntrium ennennbium |
|||
""" |
|||
fun main() { |
|||
fun String.splitToList() = trim().split("\\s+".toRegex()); |
|||
val elementsList = |
|||
elementsChunk.splitToList() |
|||
.filterNot(unamedElementsChunk.splitToList().toSet()::contains) |
|||
println("Last revision Date: ${Instant.now()}") |
|||
println("Number of elements: ${elementsList.size}") |
|||
println("Last element : ${elementsList.last()}") |
|||
println("The elements are : ${elementsList.joinToString(" ", limit = 5)}") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Last revision Date: 2021-03-09T15:27:39.396Z |
|||
Number of elements: 118 |
|||
Last element : oganesson |
|||
The elements are : hydrogen helium lithium beryllium boron ... |
|||
</pre> |
|||
=={{header|Lua}}== |
|||
<syntaxhighlight lang="lua">revised = "February 2, 2021" |
|||
-- the long literal string is delimited by double square brackets: [[...]] |
|||
-- each word must be separated by at least one whitespace character |
|||
-- additional whitespace may optionally be used to improve readability |
|||
-- (starting column does not matter, clause length is more than adequate) |
|||
longliteral = [[ |
|||
hydrogen helium lithium beryllium |
hydrogen helium lithium beryllium |
||
boron carbon nitrogen oxygen |
boron carbon nitrogen oxygen |
||
Line 456: | Line 1,303: | ||
nihonium flerovium moscovium livermorium |
nihonium flerovium moscovium livermorium |
||
tennessine oganesson |
tennessine oganesson |
||
]] |
|||
`, |
|||
-- the task requires the "final list" as single-space-between string version |
|||
elements = split(substitute_all(elements_text,"\n\r\t"," "),no_empty:=true), |
|||
-- (a more idiomatic overall approach would be to directly split into a table) |
|||
fmt = """ |
|||
finallist = longliteral:gsub("%s+"," ") |
|||
Last revision: %s |
|||
Number of elements: %d |
|||
elements = {} |
|||
The last of which is: `%s` |
|||
-- longliteral could be used here DIRECTLY instead of using finallist: |
|||
""" |
|||
for name in finallist:gmatch("%w+") do elements[#elements+1]=name end |
|||
printf(1,fmt,{last_updated,length(elements),elements[$]})</lang> |
|||
print("revised date: " .. revised) |
|||
print("# elements : " .. #elements) |
|||
print("last element: " .. elements[#elements]) |
|||
-- then, if still required, produce a single-space-between string version: |
|||
--finallist = table.concat(elements," ")</syntaxhighlight> |
|||
{{out}} |
|||
<pre>revised date: February 2, 2021 |
|||
# elements : 118 |
|||
last element: oganesson</pre> |
|||
=={{header|Nim}}== |
|||
===Using concatenations=== |
|||
<syntaxhighlight lang="nim">import strutils |
|||
const RevDate = "2021-02-05" |
|||
# We use the concatenation operator "&" to assemble the strings. |
|||
# This is done at compile time and so the result is a long literal. |
|||
const ElementString = |
|||
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine " & |
|||
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon " & |
|||
"potassium calcium scandium titanium vanadium chromium manganese iron " & |
|||
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine " & |
|||
"krypton rubidium strontium yttrium zirconium niobium molybdenum " & |
|||
"technetium ruthenium rhodium palladium silver cadmium indium tin " & |
|||
"antimony tellurium iodine xenon cesium barium lanthanum cerium " & |
|||
"praseodymium neodymium promethium samarium europium gadolinium terbium " & |
|||
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " & |
|||
"tungsten rhenium osmium iridium platinum gold mercury thallium lead " & |
|||
"bismuth polonium astatine radon francium radium actinium thorium " & |
|||
"protactinium uranium neptunium plutonium americium curium berkelium " & |
|||
"californium einsteinium fermium mendelevium nobelium lawrencium " & |
|||
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium " & |
|||
"roentgenium copernicium nihonium flerovium moscovium livermorium " & |
|||
"tennessine oganesson" |
|||
when isMainModule: |
|||
const ElementList = ElementString.split() |
|||
echo "Last revision date: ", RevDate |
|||
echo "Number of elements: ", ElementList.len |
|||
echo "Last element in list: ", ElementList[^1]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision date: 2021-02-05 |
|||
Number of elements: 118 |
|||
Last element in list: oganesson</pre> |
|||
===Using a long literal string=== |
|||
<syntaxhighlight lang="nim">import strutils |
|||
const RevDate = "2021-02-05" |
|||
# We use a long string literal starting and ending with '"""'. |
|||
# We eliminate the multiple spaces by using "splitWhiteSpace" |
|||
# instead of "split". |
|||
const ElementString = |
|||
""" |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
""" |
|||
when isMainModule: |
|||
const ElementList = ElementString.splitWhitespace() |
|||
echo "Last revision date: ", RevDate |
|||
echo "Number of elements: ", ElementList.len |
|||
echo "Last element in list: ", ElementList[^1]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision date: 2021-02-05 |
|||
Number of elements: 118 |
|||
Last element in list: oganesson</pre> |
|||
=={{header|Pascal}}== |
|||
''See [[#Free Pascal]]''<br/>The “Extended Pascal” standard (ISO 10206) defines the <tt>+</tt> string/character literal concatenation character. In “Unextended” Standard Pascal (ISO 7185) literals may not span multiple lines. |
|||
=={{header|Perl}}== |
|||
Mostly pointless... |
|||
<syntaxhighlight lang="perl">#!/usr/bin/perl |
|||
use strict; # https://rosettacode.org/wiki/Long_literals,_with_continuations |
|||
use warnings; |
|||
my $longliteral = join ' ', split ' ', <<END; |
|||
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine |
|||
neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon |
|||
potassium calcium scandium titanium vanadium chromium manganese iron cobalt |
|||
nickel copper zinc gallium germanium arsenic selenium bromine krypton rubidium |
|||
strontium yttrium zirconium niobium molybdenum technetium ruthenium rhodium |
|||
palladium silver cadmium indium tin antimony tellurium iodine xenon cesium |
|||
barium lanthanum cerium praseodymium neodymium promethium samarium europium |
|||
gadolinium terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium iridium platinum gold mercury thallium lead |
|||
bismuth polonium astatine radon francium radium actinium thorium protactinium |
|||
uranium neptunium plutonium americium curium berkelium californium einsteinium |
|||
fermium mendelevium nobelium lawrencium rutherfordium dubnium seaborgium |
|||
bohrium hassium meitnerium darmstadtium roentgenium copernicium nihonium |
|||
flerovium moscovium livermorium tennessine oganesson |
|||
END |
|||
my $version = 'Tue Feb 2 22:30:48 UTC 2021'; |
|||
my $count = my @elements = split ' ', $longliteral; |
|||
my $last = $elements[-1]; |
|||
print <<END; |
|||
version: $version |
|||
element count: $count |
|||
last element: $last |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
version: Tue Feb 2 22:30:48 UTC 2021 |
|||
element count: 118 |
|||
last element: oganesson |
|||
</pre> |
|||
=={{header|Phix}}== |
|||
{{libheader|Phix/basics}} |
|||
Back-ticks and triple-quotes permit multi-line strings. We first replace all/any cr/lf/tab characters with spaces, then split (by default on a single space), omitting empty elements. You could use spaced_elements = join(elements) to join them back up into a space-separated single string, if that's really what you want, and you could then, like Go, use rfind(' ',spaced_elements) to re-extract the last one. |
|||
You could also, like Julia, use get_file_date(command_line()[2]) instead of the hand-written last_updated constant. Phix code is free-format, indent things however you like, there is no specific maximum line length. |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">last_updated</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"March 24th, 2020"</span><span style="color: #0000FF;">,</span> |
|||
<span style="color: #000000;">elements_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">` |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
`</span><span style="color: #0000FF;">,</span> |
|||
<span style="color: #000000;">elements</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">elements_text</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\r'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\t'</span><span style="color: #0000FF;">},</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">),</span><span style="color: #000000;">no_empty</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span> |
|||
<span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""" |
|||
Last revision: %s |
|||
Number of elements: %d |
|||
The last of which is: "%s" |
|||
"""</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">last_updated</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">elements</span><span style="color: #0000FF;">),</span><span style="color: #000000;">elements</span><span style="color: #0000FF;">[$]})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Last revision: March 24th, 2020 |
Last revision: March 24th, 2020 |
||
Number of elements: 118 |
Number of elements: 118 |
||
The last of which is: |
The last of which is: "oganesson" |
||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">elements$= "hydrogen helium lithium beryllium boron carbon " + |
|||
"nitrogen oxygen fluorine neon sodium magnesium " + |
|||
"aluminum silicon phosphorous sulfur chlorine argon " + |
|||
"potassium calcium scandium titanium vanadium chromium " + |
|||
"manganese iron cobalt nickel copper zinc " + |
|||
"gallium germanium arsenic selenium bromine krypton " + |
|||
"rubidium strontium yttrium zirconium niobium molybdenum " + |
|||
"technetium ruthenium rhodium palladium silver cadmium " + |
|||
"indium tin antimony tellurium iodine xenon " + |
|||
"cesium barium lanthanum cerium praseodymium neodymium " + |
|||
"promethium samarium europium gadolinium terbium dysprosium " + |
|||
"holmium erbium thulium ytterbium lutetium hafnium " + |
|||
"tantalum tungsten rhenium osmium iridium platinum " + |
|||
"gold mercury thallium lead bismuth polonium " + |
|||
"astatine radon francium radium actinium thorium " + |
|||
"protactinium uranium neptunium plutonium americium curium " + |
|||
"berkelium californium einsteinium fermium mendelevium nobelium " + |
|||
"lawrencium rutherfordium dubnium seaborgium bohrium hassium " + |
|||
"meitnerium darmstadtium roentgenium copernicium nihonium flerovium " + |
|||
"moscovium livermorium tennessine oganesson" |
|||
revision$= "2020-11-11" |
|||
Dim result$(0) |
|||
If OpenConsole() And CreateRegularExpression(0,"[a-z]{1,18}") |
|||
nbf=ExtractRegularExpression(0,elements$,result$()) |
|||
PrintN("Last revision: "+revision$+ |
|||
~"\nNumber of elements: "+Str(nbf)+ |
|||
~"\nLast element: "+result$(nbf-1)) |
|||
Input() |
|||
EndIf</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision: 2020-11-11 |
|||
Number of elements: 118 |
|||
Last element: oganesson |
|||
</pre> |
|||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python">"""Long string literal. Requires Python 3.6+ for f-strings.""" |
|||
revision = "October 13th 2020" |
|||
# String literal continuation. Notice the trailing space at the end of each |
|||
# line but the last, and the lack of commas. There is exactly one "blank" |
|||
# between each item in the list. |
|||
elements = ( |
|||
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine " |
|||
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon " |
|||
"potassium calcium scandium titanium vanadium chromium manganese iron " |
|||
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine " |
|||
"krypton rubidium strontium yttrium zirconium niobium molybdenum " |
|||
"technetium ruthenium rhodium palladium silver cadmium indium tin " |
|||
"antimony tellurium iodine xenon cesium barium lanthanum cerium " |
|||
"praseodymium neodymium promethium samarium europium gadolinium terbium " |
|||
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " |
|||
"tungsten rhenium osmium iridium platinum gold mercury thallium lead " |
|||
"bismuth polonium astatine radon francium radium actinium thorium " |
|||
"protactinium uranium neptunium plutonium americium curium berkelium " |
|||
"californium einsteinium fermium mendelevium nobelium lawrencium " |
|||
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium " |
|||
"roentgenium copernicium nihonium flerovium moscovium livermorium " |
|||
"tennessine oganesson" |
|||
) |
|||
def report(): |
|||
"""Write a summary report to stdout.""" |
|||
items = elements.split() |
|||
print(f"Last revision date: {revision}") |
|||
print(f"Number of elements: {len(items)}") |
|||
print(f"Last element : {items[-1]}") |
|||
if __name__ == "__main__": |
|||
report() |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Last revision date: October 13th 2020 |
|||
Number of elements: 118 |
|||
Last element : oganesson |
|||
</pre> |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery"> |
|||
[ stack ] is last-revision ( --> $ ) |
|||
$ "6 Jun 2021" last-revision put |
|||
[ stack ] is elements ( --> $ ) |
|||
$ " hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson" |
|||
nest$ |
|||
$ "" swap |
|||
witheach [ join space join ] |
|||
-1 split drop |
|||
elements put |
|||
[ last-revision share decho$ ] is echorevision ( --> ) |
|||
[ elements share nest$ size ] is elementcount ( --> n ) |
|||
[ elements share |
|||
reverse nextword |
|||
nip reverse ] is finalelement ( --> $ ) |
|||
say "Last revision: " echorevision cr |
|||
say "Number of elements: " elementcount echo cr |
|||
say "Last element: " finalelement echo$ cr |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last revision: 6 Jun 2021 |
|||
Number of elements: 118 |
|||
Last element: oganesson</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 480: | Line 1,673: | ||
Not really sure I understand the point of this task. Seems to be load some list into memory and manipulate it somehow. Exceptionally boring to just read it in and then read it back out again. Perform some more interesting manipulations. Use < > quoting construct for literal string; unlimited (memory limited) characters, spaces don't matter, new-lines don't matter, blank lines don't matter. |
Not really sure I understand the point of this task. Seems to be load some list into memory and manipulate it somehow. Exceptionally boring to just read it in and then read it back out again. Perform some more interesting manipulations. Use < > quoting construct for literal string; unlimited (memory limited) characters, spaces don't matter, new-lines don't matter, blank lines don't matter. |
||
<lang |
<syntaxhighlight lang="raku" line>my %periodic; |
||
%periodic<revision-date> = Date.new(2020,3,23); |
%periodic<revision-date> = Date.new(2020,3,23); |
||
%periodic<table> = |< |
%periodic<table> = |< |
||
Line 554: | Line 1,747: | ||
put 'Symbols for elements whose name starts with "P": ', %periodic<table>.grep( *.<name>.starts-with('P') )».<symbol>; |
put 'Symbols for elements whose name starts with "P": ', %periodic<table>.grep( *.<name>.starts-with('P') )».<symbol>; |
||
put "Elements with molecular weight between 20 & 40:\n ",%periodic<table>.grep( {+.<weight> ~~ Numeric and 20 < .<weight> < 40} )».<name>; |
put "Elements with molecular weight between 20 & 40:\n ",%periodic<table>.grep( {+.<weight> ~~ Numeric and 20 < .<weight> < 40} )».<name>; |
||
put "SCRN: ", %periodic<table>[87,17,92]».<symbol>.join.tclc;</ |
put "SCRN: ", %periodic<table>[87,17,92]».<symbol>.join.tclc;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Revision date: 2020-03-23 |
<pre>Revision date: 2020-03-23 |
||
Line 575: | Line 1,768: | ||
Most modern REXXes have no practical limit for a clause length. |
Most modern REXXes have no practical limit for a clause length. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm illustrates how to code a list of words (named chemical elements */ |
||
/*──────────────────────── ordered by their atomic number) in a list format. */ |
/*──────────────────────── ordered by their atomic number) in a list format. */ |
||
Line 606: | Line 1,799: | ||
#= words(elements) /*the number of elements " " " */ |
#= words(elements) /*the number of elements " " " */ |
||
say 'number of elements in the list: ' # /*show " " " " " " */ |
say 'number of elements in the list: ' # /*show " " " " " " */ |
||
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/</ |
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 624: | Line 1,817: | ||
The REXX version uses concatenation (also called abutment) to build the list. |
The REXX version uses concatenation (also called abutment) to build the list. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm illustrates how to code a list of words (named chemical elements */ |
||
/*──────────────────────── ordered by their atomic number) in a list format. */ |
/*──────────────────────── ordered by their atomic number) in a list format. */ |
||
Line 655: | Line 1,848: | ||
#= words(elements) /*the number of elements " " " */ |
#= words(elements) /*the number of elements " " " */ |
||
say 'number of elements in the list: ' # /*show " " " " " " */ |
say 'number of elements in the list: ' # /*show " " " " " " */ |
||
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/</ |
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 662: | Line 1,855: | ||
the last element is: oganesson |
the last element is: oganesson |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
≪ { "hydrogen" "helium" "lithium" "beryllium" "boron" "carbon" "nitrogen" "oxygen" "fluorine" "neon" "sodium" "magnesium" "aluminum" "silicon" "phosphorous" "sulfur" "chlorine" "argon" "potassium" "calcium" "scandium" "titanium" "vanadium" "chromium" "manganese" "iron" "cobalt" "nickel" "copper" "zinc" "gallium" "germanium" "arsenic" "selenium" "bromine" "krypton" "rubidium" "strontium" "yttrium" "zirconium" "niobium" "molybdenum" "technetium" "ruthenium" "rhodium" "palladium" "silver" "cadmium" "indium" "tin" "antimony" "tellurium" "iodine" "xenon" "cesium" "barium" "lanthanum" "cerium" "praseodymium" "neodymium" "promethium" "samarium" "europium" "gadolinium" "terbium" "dysprosium" "holmium" "erbium" "thulium" "ytterbium" "lutetium" "hafnium" "tantalum" "tungsten" "rhenium" "osmium" "iridium" "platinum" "gold" "mercury" "thallium" "lead" "bismuth" "polonium" "astatine" "radon" "francium" "radium" "actinium" "thorium" "protactinium" "uranium" "neptunium" "plutonium" "americium" "curium" "berkelium" "californium" "einsteinium" "fermium" "mendelevium" "nobelium" "lawrencium" "rutherfordium" "dubnium" "seaborgium" "bohrium" "hassium" "meitnerium" "darmstadtium" "roentgenium" "copernicium" "nihonium" "flerovium" "moscovium" "livermorium" "tennessine" "oganesson" } "23/02/05" → elements updated |
|||
≪ "Updated: " updated + |
|||
"# items: " elements SIZE →STR + |
|||
"Last : " elements DUP SIZE GET + |
|||
≫ ≫ 'ELMTS' STO |
|||
ELMTS |
|||
{{out}} |
|||
<pre> |
|||
3: "Updated: 23/02/05" |
|||
2: "# items: 118" |
|||
1: "Last : oganesson" |
|||
</pre> |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby">elements = %w( |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson) |
|||
puts "Last mutation #{ File.mtime(__FILE__) } |
|||
number of elements: #{elements.size} |
|||
last element: #{elements.last}" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last mutation 2021-08-30 17:14:16 +0200 |
|||
number of elements: 118 |
|||
last element: oganesson |
|||
</pre> |
|||
=={{header|Standard ML}}== |
|||
<syntaxhighlight lang="sml">(* space in \ .. \ is ignored *) |
|||
val elements = "\ |
|||
\hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine \ |
|||
\neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon \ |
|||
\potassium calcium scandium titanium vanadium chromium manganese iron cobalt \ |
|||
\nickel copper zinc gallium germanium arsenic selenium bromine krypton \ |
|||
\rubidium strontium yttrium zirconium niobium molybdenum technetium \ |
|||
\ruthenium rhodium palladium silver cadmium indium tin antimony tellurium \ |
|||
\iodine xenon cesium barium lanthanum cerium praseodymium neodymium \ |
|||
\promethium samarium europium gadolinium terbium dysprosium holmium erbium \ |
|||
\thulium ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium \ |
|||
\platinum gold mercury thallium lead bismuth polonium astatine radon \ |
|||
\francium radium actinium thorium protactinium uranium neptunium plutonium \ |
|||
\americium curium berkelium californium einsteinium fermium mendelevium \ |
|||
\nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium \ |
|||
\meitnerium darmstadtium roentgenium copernicium nihonium flerovium \ |
|||
\moscovium livermorium tennessine oganesson" |
|||
fun report lst = print ("\ |
|||
\revision date:\t2021-02-25\n\ |
|||
\element count:\t" ^ Int.toString (length lst) ^ "\n\ |
|||
\last element:\t" ^ List.last lst ^ "\n") |
|||
val () = report (String.fields (fn c => c = #" ") elements)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>revision date: 2021-02-25 |
|||
element count: 118 |
|||
last element: oganesson</pre> |
|||
=={{header|Tcl}}== |
|||
Using the neatly formatted table from other languages (can be reformatted easily using the non-standard utility column with option -t). |
|||
<syntaxhighlight lang="tcl">set mtime "2021-05-10 16:11:50" |
|||
set elements { |
|||
hydrogen helium lithium beryllium |
|||
boron carbon nitrogen oxygen |
|||
fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur |
|||
chlorine argon potassium calcium |
|||
scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel |
|||
copper zinc gallium germanium |
|||
arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium |
|||
niobium molybdenum technetium ruthenium |
|||
rhodium palladium silver cadmium |
|||
indium tin antimony tellurium |
|||
iodine xenon cesium barium |
|||
lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium |
|||
terbium dysprosium holmium erbium |
|||
thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium |
|||
iridium platinum gold mercury |
|||
thallium lead bismuth polonium |
|||
astatine radon francium radium |
|||
actinium thorium protactinium uranium |
|||
neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium |
|||
mendelevium nobelium lawrencium rutherfordium |
|||
dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium |
|||
nihonium flerovium moscovium livermorium |
|||
tennessine oganesson |
|||
} |
|||
set elements [list {*}$elements]; # Normalize string representation |
|||
puts "Last modification time: $mtime" |
|||
puts "Number of elements: [llength $elements]" |
|||
puts "Last element: [lindex $elements end]"</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Last modification time: 2021-05-10 16:11:50 |
|||
Number of elements: 118 |
|||
Last element: oganesson</pre> |
|||
=={{header|UNIX Shell}}== |
|||
{{works with|Bourne Again Shell}} |
|||
{{works with|Korn Shell}} |
|||
{{works with|Zsh}} |
|||
These modern shells have arrays, which are easily initialized across multiple lines. While |
|||
has line continuations with `\\` are available, the parentheses enclosing the array initializer |
|||
are sufficient to extend it across as many lines as needed: |
|||
<syntaxhighlight lang="sh"> |
|||
main() { |
|||
elements=( |
|||
hydrogen helium lithium beryllium boron carbon |
|||
nitrogen oxygen fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur chlorine argon |
|||
potassium calcium scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel copper zinc |
|||
gallium germanium arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium niobium molybdenum |
|||
technetium ruthenium rhodium palladium silver cadmium |
|||
indium tin antimony tellurium iodine xenon |
|||
cesium barium lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium terbium dysprosium |
|||
holmium erbium thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium iridium platinum |
|||
gold mercury thallium lead bismuth polonium |
|||
astatine radon francium radium actinium thorium |
|||
protactinium uranium neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium mendelevium nobelium |
|||
lawrencium rutherfordium dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium nihonium flerovium |
|||
moscovium livermorium tennessine oganesson) |
|||
updated=2021-08-30 |
|||
printf 'Last update: %s\n' "$updated" |
|||
printf 'Element count: %d\n' ${#elements[@]} |
|||
printf 'Latest element: %s\n' "${elements[-1]}" |
|||
} |
|||
main "$@"</syntaxhighlight> |
|||
{{Out}} |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-pattern}} |
{{libheader|Wren-pattern}} |
||
We use a 'raw' string for this task. |
|||
Wren doesn't have raw strings nor continuations and so we concatenate rows of strings instead. |
|||
<syntaxhighlight lang="wren">import "./pattern" for Pattern |
|||
The rows can begin in any column though, for a tidy display, we begin them all here in column 5. |
|||
<lang ecmascript>import "/pattern" for Pattern |
|||
var elementStr = |
var elementStr = |
||
"""hydrogen helium lithium beryllium boron carbon |
|||
nitrogen oxygen fluorine neon sodium magnesium |
|||
aluminum silicon phosphorous sulfur chlorine argon |
|||
potassium calcium scandium titanium vanadium chromium |
|||
manganese iron cobalt nickel copper zinc |
|||
gallium germanium arsenic selenium bromine krypton |
|||
rubidium strontium yttrium zirconium niobium molybdenum |
|||
technetium ruthenium rhodium palladium silver cadmium |
|||
indium tin antimony tellurium iodine xenon |
|||
cesium barium lanthanum cerium praseodymium neodymium |
|||
promethium samarium europium gadolinium terbium dysprosium |
|||
holmium erbium thulium ytterbium lutetium hafnium |
|||
tantalum tungsten rhenium osmium iridium platinum |
|||
gold mercury thallium lead bismuth polonium |
|||
astatine radon francium radium actinium thorium |
|||
protactinium uranium neptunium plutonium americium curium |
|||
berkelium californium einsteinium fermium mendelevium nobelium |
|||
lawrencium rutherfordium dubnium seaborgium bohrium hassium |
|||
meitnerium darmstadtium roentgenium copernicium nihonium flerovium |
|||
moscovium livermorium tennessine oganesson""" |
|||
var p = Pattern.new("+1/s") // matches 1 or more whitespace characters |
var p = Pattern.new("+1/s") // matches 1 or more whitespace characters |
||
var elements = p.splitAll(elementStr) // get a list of elements |
var elements = p.splitAll(elementStr) // get a list of elements |
||
elementStr = elements.join(" ") // recombine using a single space as separator |
elementStr = elements.join(" ") // recombine using a single space as separator |
||
var lastUpdate = " |
var lastUpdate = "2023-12-17" |
||
System.print("Last updated : %(lastUpdate)") |
System.print("Last updated : %(lastUpdate)") |
||
System.print("Number of elements : %(elements.count)") |
System.print("Number of elements : %(elements.count)") |
||
System.print("Last element : %(elements[-1])")</ |
System.print("Last element : %(elements[-1])")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Last updated : |
Last updated : 2023-12-17 |
||
Number of elements : 118 |
Number of elements : 118 |
||
Last element : oganesson |
Last element : oganesson |
||
Line 715: | Line 2,084: | ||
(by default). To turn that into one string with one space between each item, |
(by default). To turn that into one string with one space between each item, |
||
use: elements.concat(" ") |
use: elements.concat(" ") |
||
< |
<syntaxhighlight lang="zkl">revisionDate:="2020-03-23"; |
||
elements:= |
elements:= |
||
#<<<" |
#<<<" |
||
Line 741: | Line 2,110: | ||
#<<< |
#<<< |
||
println("Revision date: ",revisionDate); |
println("Revision date: ",revisionDate); |
||
println(elements.len()," elements, the last being \"",elements[-1],"\"");</ |
println(elements.len()," elements, the last being \"",elements[-1],"\"");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 16:04, 17 December 2023
You are encouraged to solve this task according to the task description, using any language you may know.
This task is about writing a computer program that has long literals (character literals that may require specifying the words/tokens on more than one (source) line, either with continuations or some other method, such as abutments or concatenations (or some other mechanisms).
The literal is to be in the form of a "list", a literal that contains many
words (tokens) separated by a blank (space), in this case (so as to have a
common list), the (English) names of the chemical elements of the periodic table.
The list is to be in (ascending) order of the (chemical) element's atomic number:
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon sodium aluminum silicon ...
... up to the last known (named) chemical element (at this time).
Do not include any of the "unnamed" chemical element names such as:
ununennium unquadnilium triunhexium penthextrium penthexpentium septhexunium octenntrium ennennbium
To make computer programming languages comparable, the statement widths should be
restricted to less than 81 bytes (characters), or less
if a computer programming language has more restrictive limitations or standards.
Also mention what column the programming statements can start in if not in column one.
The list may have leading/embedded/trailing blanks during the
declaration (the actual program statements), this is allow the list to be
more readable. The "final" list shouldn't have any leading/trailing or superfluous
blanks (when stored in the program's "memory").
This list should be written with the idea in mind that the program will be updated, most likely someone other than the original author, as there will be newer (discovered) elements of the periodic table being added (possibly in the near future). These future updates should be one of the primary concerns in writing these programs and it should be "easy" for someone else to add chemical elements to the list (within the computer program).
Attention should be paid so as to not exceed the clause length of continued or specified statements, if there is such a restriction. If the limit is greater than (say) 4,000 bytes or so, it needn't be mentioned here.
- Task
-
- Write a computer program (by whatever name) to contain a list of the known elements.
- The program should eventually contain a long literal of words (the elements).
- The literal should show how one could create a long list of blank-delineated words.
- The "final" (stored) list should only have a single blank between elements.
- Try to use the most idiomatic approach(es) in creating the final list.
- Use continuation if possible, and/or show alternatives (possibly using concatenation).
- Use a program comment to explain what the continuation character is if it isn't obvious.
- The program should contain a variable that has the date of the last update/revision.
- The program, when run, should display with verbiage:
- The last update/revision date (and should be unambiguous).
- The number of chemical elements in the list.
- The name of the highest (last) element name.
Show all output here, on this page.
- Metrics
- Counting
- Word frequency
- Letter frequency
- Jewels and stones
- I before E except after C
- Bioinformatics/base count
- Count occurrences of a substring
- Count how many vowels and consonants occur in a string
- Remove/replace
- XXXX redacted
- Conjugate a Latin verb
- Remove vowels from a string
- String interpolation (included)
- Strip block comments
- Strip comments from a string
- Strip a set of characters from a string
- Strip whitespace from a string -- top and tail
- Strip control codes and extended characters from a string
- Anagrams/Derangements/shuffling
- Word wheel
- ABC problem
- Sattolo cycle
- Knuth shuffle
- Ordered words
- Superpermutation minimisation
- Textonyms (using a phone text pad)
- Anagrams
- Anagrams/Deranged anagrams
- Permutations/Derangements
- Find/Search/Determine
- ABC words
- Odd words
- Word ladder
- Semordnilap
- Word search
- Wordiff (game)
- String matching
- Tea cup rim text
- Alternade words
- Changeable words
- State name puzzle
- String comparison
- Unique characters
- Unique characters in each string
- Extract file extension
- Levenshtein distance
- Palindrome detection
- Common list elements
- Longest common suffix
- Longest common prefix
- Compare a list of strings
- Longest common substring
- Find common directory path
- Words from neighbour ones
- Change e letters to i in words
- Non-continuous subsequences
- Longest common subsequence
- Longest palindromic substrings
- Longest increasing subsequence
- Words containing "the" substring
- Sum of the digits of n is substring of n
- Determine if a string is numeric
- Determine if a string is collapsible
- Determine if a string is squeezable
- Determine if a string has all unique characters
- Determine if a string has all the same characters
- Longest substrings without repeating characters
- Find words which contains all the vowels
- Find words which contains most consonants
- Find words which contains more than 3 vowels
- Find words which first and last three letters are equals
- Find words which odd letters are consonants and even letters are vowels or vice_versa
- Formatting
- Substring
- Rep-string
- Word wrap
- String case
- Align columns
- Literals/String
- Repeat a string
- Brace expansion
- Brace expansion using ranges
- Reverse a string
- Phrase reversals
- Comma quibbling
- Special characters
- String concatenation
- Substring/Top and tail
- Commatizing numbers
- Reverse words in a string
- Suffixation of decimal numbers
- Long literals, with continuations
- Numerical and alphabetical suffixes
- Abbreviations, easy
- Abbreviations, simple
- Abbreviations, automatic
- Song lyrics/poems/Mad Libs/phrases
- Mad Libs
- Magic 8-ball
- 99 Bottles of Beer
- The Name Game (a song)
- The Old lady swallowed a fly
- The Twelve Days of Christmas
- Tokenize
- Text between
- Tokenize a string
- Word break problem
- Tokenize a string with escaping
- Split a character string based on change of character
- Sequences
11l
V revdate = ‘2021-11-14’
V elements =
‘hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine ’""
‘neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon ’""
‘potassium calcium scandium titanium vanadium chromium manganese iron ’""
‘cobalt nickel copper zinc gallium germanium arsenic selenium bromine ’""
‘krypton rubidium strontium yttrium zirconium niobium molybdenum ’""
‘technetium ruthenium rhodium palladium silver cadmium indium tin ’""
‘antimony tellurium iodine xenon cesium barium lanthanum cerium ’""
‘praseodymium neodymium promethium samarium europium gadolinium terbium ’""
‘dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum ’""
‘tungsten rhenium osmium iridium platinum gold mercury thallium lead ’""
‘bismuth polonium astatine radon francium radium actinium thorium ’""
‘protactinium uranium neptunium plutonium americium curium berkelium ’""
‘californium einsteinium fermium mendelevium nobelium lawrencium ’""
‘rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium ’""
‘roentgenium copernicium nihonium flerovium moscovium livermorium ’""
‘tennessine oganesson’
V items = elements.split(‘ ’)
print(‘Last revision date: ’revdate)
print(‘Number of elements: ’items.len)
print(‘Last element : ’items.last)
- Output:
Last revision date: 2021-11-14 Number of elements: 118 Last element : oganesson
6502 Assembly
There is no need for continuation codes, as the language interprets a contiguous data block as a single entity, whether you intended it to be or not. If you had two consecutive strings embedded in the code and you forgot the null terminator on the first one, trying to print just the first string would print both. For example:
HelloString:
db "Hello World" ;no null terminator
GoodbyeString:
db "Goodbye World!",0
PrintString HelloString ;unimplemented macro.
The output would be as follows, assuming the PrintString
routine uses a null terminator to know when a string ends:
Hello WorldGoodbye World!
In addition, it makes no difference to the assembler whether each string is on its own line, or each is one after the other on the same line. It's easier to put each on its own line since you can't have a label mid-line.
Since each string is of variable length, it is much easier to make a lookup table of the elements ordered by atomic number, with each element in the lookup table being a pointer to the actual string consisting of that element's name.
ElementNull:
dw nullString
Elements:
; this is typed in a compact manner to save on typing, however putting each on its own line with a
; "dw" directive in front will produce the same result. A comment with the element number on each line will aid in
; adding new elements to the list.
dw hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine
dw neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon
dw potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron
dw cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine
dw krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum
dw technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin
dw antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium
dw praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium
dw dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum
dw tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead
dw bismuth,polonium,astatine,radon,francium,radium,actinium,thorium
dw protactinium,uranium,neptunium,plutonium,americium,curium,berkelium
dw californium,einsteinium,fermium,mendelevium,nobelium,lawrencium
dw rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium
dw roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium
dw tennessine,oganesson
Elements_End:
nullString:
db 0
hydrogen:
db "hydrogen",0
helium:
db "helium",0
lithium:
db "lithium",0
;etc.
RevisionDate:
db "2021-Sep-20th",0
Finally:
db "elements, the last is",0
ElementCount equ (Elements_End-Elements)/2
; a constant value that cannot change at runtime.
; This counts the number of bytes between the two labels, and automatically adjusts when the size of the list changes.
; The division by 2 gets the actual element count since each address is 2 bytes long.
The required output can be obtained like so:
LDA #<RevisionDate ;get the low byte of the address
STA z_L ;store it in z_L, a zero page memory address
LDA #>RevisionDate ;get the high byte
STA z_H ;store it in z_H, the zero page memory address directly after z_L
JSR PrintString ;unimplemented printing routine
JSR NewLine
LDA ElementCount
JSR ConvertHex2BinDec ;converts a hexadecimal value to a trio of BCD digits
JSR PrintBCD ;unimplemented printing routine for numeric values
LDA #$20 ;ASCII for spacebar
JSR PrintChar
LDA #<Finally
STA z_L
LDA #>Finally
STA z_H
JSR PrintString
LDA #$20 ;ASCII for spacebar
JSR PrintChar
LDA ElementCount
ASL A ;multiply by 2, we are indexing into a table of words.
TAX ;use as an offset into the lookup table.
LDA ElementNull,x ;ElementCount doesn't account for zero-indexing so we'll need to load one word behind.
STA z_L
LDA ElementNull+1,x
STA z_H
JSR PrintString
This routine can be expanded as new elements are discovered, but once the 128th element is discovered it will need to be reprogrammed since you can't offset more than x=255. This is actually a very simple fix, and can be accomplished by splitting the table as such:
Elements_Lo:
db <hydrogen,<helium,<lithium,<beryllium,<boron,<carbon,<nitrogen,<oxygen,<fluorine,...
Elements_Hi:
db >hydrogen,>helium,>lithium,>beryllium,>boron,>carbon,>nitrogen,>oxygen,>fluorine,...
Note that < and > are unary operators that mean "the low byte of" and "the high byte of", respectively. By storing the halves of each pointer in two separate tables, sharing a common index, we can index up to 256 elements rather than 128, without increasing the total data size of the tables.
- Output:
2021-Sep-20th 118 elements, the last is oganesson
68000 Assembly
There is no need for continuation codes, as the language interprets a contiguous data block as a single entity, whether you intended it to be or not. If you had two consecutive strings embedded in the code and you forgot the null terminator on the first one, trying to print just the first string would print both. For example:
HelloString:
DC.B "Hello World" ;no null terminator
GoodbyeString:
DC.B "Goodbye World!",0
EVEN
PrintString HelloString ;unimplemented macro.
The output would be as follows, assuming the PrintString
routine uses a null terminator to know when a string ends:
Hello WorldGoodbye World!
In addition, it makes no difference to the assembler whether each string is on its own line, or each is one after the other on the same line. It's easier to put each on its own line since you can't have a label mid-line.
Since each string is of variable length, it is much easier to make a lookup table of the elements ordered by atomic number, with each element in the lookup table being a pointer to the actual string consisting of that element's name.
NullElement:
DC.L nullString
Elements:
; this is typed in a compact manner to save on typing, however putting each on its own line with a
; "DC.L" directive in front will produce the same result. A comment with the element number on each line will aid in
; adding new elements to the list.
DC.L hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine
DC.L neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon
DC.L potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron
DC.L cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine
DC.L krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum
DC.L technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin
DC.L antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium
DC.L praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium
DC.L dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum
DC.L tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead
DC.L bismuth,polonium,astatine,radon,francium,radium,actinium,thorium
DC.L protactinium,uranium,neptunium,plutonium,americium,curium,berkelium
DC.L californium,einsteinium,fermium,mendelevium,nobelium,lawrencium
DC.L rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium
DC.L roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium
DC.L tennessine,oganesson
Elements_End:
nullString:
DC.B 0
EVEN
hydrogen:
DC.B "hydrogen",0
EVEN
helium:
DC.B "helium",0
EVEN
lithium:
DC.B "lithium",0
EVEN
;etc.
RevisionDate:
DC.B "2021-Sep-20th",0
EVEN
Finally:
DC.B "elements, the last is",0
EVEN
ElementCount equ (Elements_End-Elements)/4
; a constant value that cannot change at runtime.
; This counts the number of bytes between the two labels, and automatically adjusts when the size of the list changes.
; The division by 4 gets the actual element count since each address is 4 bytes long.
The required output can be obtained like so:
LEA RevisionDate,A3 ; the printing routine uses A3 as input
JSR PrintString ; unimplemented printing routine
JSR NewLine ; unimplemented new line routine
MOVE.B ElementCount,D0
JSR ConvertHex2BinDec ; converts a hexadecimal value to a trio of BCD digits
JSR PrintBCD ; unimplemented printing routine for numeric values
MOVE.B #' ',D0 ; ASCII for spacebar
JSR PrintChar
LEA Finally,A3
JSR PrintString
MOVE.B #' ',D0 ; ASCII for spacebar
JSR PrintChar
MOVE.W ElementCount,D1
LSL.W #2,D1 ; multiply by 4, we are indexing into a table of longs
LEA NullElement,A2
; load the base of the lookup table into A2. This is the table's "true base"
; since the "ElementCount" constant doesn't account for zero-indexing
MOVEA.L (A2,D1),A3
;dereference the pointer, offsetting by D1. This retrieves the address of the desired element in the list.
JSR PrintString
This routine can easily be expanded for more elements. With a maximum offset of 16,383, it's unlikely that the table will get too large to properly index anytime soon.
- Output:
2021-Sep-20th 118 elements, the last is oganesson
Arturo
Using concatenations
revDate: "2021-02-05"
elementString:
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine " ++
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon " ++
"potassium calcium scandium titanium vanadium chromium manganese iron " ++
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine " ++
"krypton rubidium strontium yttrium zirconium niobium molybdenum " ++
"technetium ruthenium rhodium palladium silver cadmium indium tin " ++
"antimony tellurium iodine xenon cesium barium lanthanum cerium " ++
"praseodymium neodymium promethium samarium europium gadolinium terbium " ++
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " ++
"tungsten rhenium osmium iridium platinum gold mercury thallium lead " ++
"bismuth polonium astatine radon francium radium actinium thorium " ++
"protactinium uranium neptunium plutonium americium curium berkelium " ++
"californium einsteinium fermium mendelevium nobelium lawrencium " ++
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium " ++
"roentgenium copernicium nihonium flerovium moscovium livermorium " ++
"tennessine oganesson"
elements: split.words elementString
print ["Last revision date:" revDate]
print ["Number of elements:" size elements]
print ["Last element in list:" last elements]
- Output:
Last revision date: 2021-02-05 Number of elements: 118 Last element in list: oganesson
Using string blocks
revDate: "2021-02-05"
elementString: {
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine
neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon
potassium calcium scandium titanium vanadium chromium manganese iron
cobalt nickel copper zinc gallium germanium arsenic selenium bromine
krypton rubidium strontium yttrium zirconium niobium molybdenum
technetium ruthenium rhodium palladium silver cadmium indium tin
antimony tellurium iodine xenon cesium barium lanthanum cerium
praseodymium neodymium promethium samarium europium gadolinium terbium
dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum
tungsten rhenium osmium iridium platinum gold mercury thallium lead
bismuth polonium astatine radon francium radium actinium thorium
protactinium uranium neptunium plutonium americium curium berkelium
californium einsteinium fermium mendelevium nobelium lawrencium
rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium
roentgenium copernicium nihonium flerovium moscovium livermorium
tennessine oganesson
}
elements: split.words elementString
print ["Last revision date:" revDate]
print ["Number of elements:" size elements]
print ["Last element in list:" last elements]
- Output:
Last revision date: 2021-02-05 Number of elements: 118 Last element in list: oganesson
AWK
# syntax: GAWK -f LONG_LITERALS_WITH_CONTINUATIONS.AWK
BEGIN {
ll_using_concatenation() ; ll_info()
ll_using_continuation() ; ll_info()
exit(0)
}
function ll_info( arr,n,x) {
n = split(str,arr," ")
printf("version: %s\n",revised)
printf("number of elements: %d\n",n)
printf("last element: %s\n",arr[n])
x = 30
printf("first & last %d characters: %s & %s\n\n",x,substr(str,1,x),substr(str,length(str)-x))
}
function ll_remove_multiple_spaces(s) {
# all element names were wrapped in one or more spaces for readability and ease of future editing
# they are removed here
gsub(/\n/," ",s) # AWK95 needs, GAWK & TAWK don't
while (s ~ / /) {
gsub(/ +/," ",s)
}
sub(/^ /,"",s)
sub(/ $/,"",s)
return(s)
}
function ll_using_concatenation( s) {
s=s" hydrogen helium lithium beryllium "
s=s" boron carbon nitrogen oxygen "
s=s" fluorine neon sodium magnesium "
s=s" aluminum silicon phosphorous sulfur "
s=s" chlorine argon potassium calcium "
s=s" scandium titanium vanadium chromium "
s=s" manganese iron cobalt nickel "
s=s" copper zinc gallium germanium "
s=s" arsenic selenium bromine krypton "
s=s" rubidium strontium yttrium zirconium "
s=s" niobium molybdenum technetium ruthenium "
s=s" rhodium palladium silver cadmium "
s=s" indium tin antimony tellurium "
s=s" iodine xenon cesium barium "
s=s" lanthanum cerium praseodymium neodymium "
s=s" promethium samarium europium gadolinium "
s=s" terbium dysprosium holmium erbium "
s=s" thulium ytterbium lutetium hafnium "
s=s" tantalum tungsten rhenium osmium "
s=s" iridium platinum gold mercury "
s=s" thallium lead bismuth polonium "
s=s" astatine radon francium radium "
s=s" actinium thorium protactinium uranium "
s=s" neptunium plutonium americium curium "
s=s" berkelium californium einsteinium fermium "
s=s" mendelevium nobelium lawrencium rutherfordium "
s=s" dubnium seaborgium bohrium hassium "
s=s" meitnerium darmstadtium roentgenium copernicium "
s=s" nihonium flerovium moscovium livermorium "
s=s" tennessine oganesson "
str = ll_remove_multiple_spaces(s)
revised = "2020-06-30"
}
function ll_using_continuation( s) {
# works with: AWK95, GAWK 3.1.4, GAWK 5, TAWK
s="\
hydrogen helium lithium beryllium \
boron carbon nitrogen oxygen \
fluorine neon sodium magnesium \
aluminum silicon phosphorous sulfur \
chlorine argon potassium calcium \
scandium titanium vanadium chromium \
manganese iron cobalt nickel \
copper zinc gallium germanium \
arsenic selenium bromine krypton \
rubidium strontium yttrium zirconium \
niobium molybdenum technetium ruthenium \
rhodium palladium silver cadmium \
indium tin antimony tellurium \
iodine xenon cesium barium \
lanthanum cerium praseodymium neodymium \
promethium samarium europium gadolinium \
terbium dysprosium holmium erbium \
thulium ytterbium lutetium hafnium \
tantalum tungsten rhenium osmium \
iridium platinum gold mercury \
thallium lead bismuth polonium \
astatine radon francium radium \
actinium thorium protactinium uranium \
neptunium plutonium americium curium \
berkelium californium einsteinium fermium \
mendelevium nobelium lawrencium rutherfordium \
dubnium seaborgium bohrium hassium \
meitnerium darmstadtium roentgenium copernicium \
nihonium flerovium moscovium livermorium \
tennessine oganesson \
"
str = ll_remove_multiple_spaces(s)
revised = "30JUN2020"
}
- Output:
version: 2020-06-30 number of elements: 118 last element: oganesson first & last 30 characters: hydrogen helium lithium beryll & ivermorium tennessine oganesson version: 30JUN2020 number of elements: 118 last element: oganesson first & last 30 characters: hydrogen helium lithium beryll & ivermorium tennessine oganesson
BASIC256
ultimaRevision$ = "2021-11-12"
arraybase 1
dim elemento$ = {"hydrogen", "helium", "lithium", "beryllium", "boron", "carbon", "nitrogen", "oxygen", "fluorine", "neon", "sodium", "magnesium", "aluminum", "silicon", "phosphorous", "sulfur", "chlorine", "argon", "potassium", "calcium", "scandium", "titanium", "vanadium", "chromium", "manganese", "iron", "cobalt", "nickel", "copper", "zinc", "gallium", "germanium", "arsenic", "selenium", "bromine", "krypton", "rubidium", "strontium", "yttrium", "zirconium", "niobium", "molybdenum", "technetium", "ruthenium", "rhodium", "palladium", "silver", "cadmium", "indium", "tin", "antimony", "tellurium", "iodine", "xenon", "cesium", "barium", "lanthanum", "cerium", "praseodymium", "neodymium", "promethium", "samarium", "europium", "gadolinium", "terbium", "dysprosium", "holmium", "erbium", "thulium", "ytterbium", "lutetium", "hafnium", "tantalum", "tungsten", "rhenium", "osmium", "iridium", "platinum", "gold", "mercury", "thallium", "lead", "bismuth", "polonium", "astatine", "radon", "francium", "radium", "actinium", "thorium", "protactinium", "uranium", "neptunium", "plutonium", "americium", "curium", "berkelium", "californium", "einsteinium", "fermium", "mendelevium", "nobelium", "lawrencium", "rutherfordium", "dubnium", "seaborgium", "bohrium", "hassium", "meitnerium", "darmstadtium", "roentgenium", "copernicium", "nihonium", "flerovium", "moscovium", "livermorium", "tennessine", "oganesson"}
print "Last updated : "; ultimaRevision$
print "Number of elements : "; elemento$[?]
print "Last element : "; elemento$[elemento$[?]]
end
- Output:
Igual que la entrada de FreeBASIC.
C++
#include <chrono>
#include <format>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
const std::string ELEMENTS = R"(
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
)";
const std::string UNNAMED_ELEMENTS = R"(
ununennium unquadnilium triunhexium penthextrium
penthexpentium septhexunium octenntrium ennennbium
)";
std::vector<std::string> rawstring_to_vector(const std::string& text, const char& delimiter) {
std::regex regx("\\s+");
std::string delimit(1, delimiter);
std::string elements = std::regex_replace(text, regx, delimit);
elements = elements.substr(1, elements.size() - 2);
std::vector<std::string> result;
std::stringstream stream(elements);
std::string item;
while ( getline(stream, item, delimiter) ) {
result.push_back (item);
}
return result;
}
int main() {
std::vector<std::string> elements = rawstring_to_vector(ELEMENTS, ' ');
std::vector<std::string> unnamed = rawstring_to_vector(UNNAMED_ELEMENTS, ' ');
elements.erase(std::remove_if(elements.begin(), elements.end(),
[unnamed](std::string text){ return std::find(unnamed.begin(), unnamed.end(), text) != unnamed.end(); }),
elements.end());
const std::string zone = "Asia/Shanghai";
const std::chrono::zoned_time zoned_time { zone, std::chrono::system_clock::now() };
std::cout << "Last revision Date: " << std::format("{:%Y-%m-%d Time %H:%M}", zoned_time)
<< " " << zone << std::endl;
std::cout << "Number of elements: " << elements.size() << std::endl;
std::cout << "Last element : " << elements[elements.size() - 1] << std::endl;
}
- Output:
Last revision Date: 2023-08-04 Time 00:18 Asia/Shanghai Number of elements: 118 Last element : oganesson
Crystal
Crystal's %w
literals create a whitespace-delimited array of strings at compile time
require "time"
last_revision = Time.utc year: 2021, month: 2, day: 25
# the `%w()` literal creates an array from a whitespace-delimited string literal
# it's equivalent to %(string literal).split
# https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html
element_list : Array(String) = %w(
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson)
puts last_revision.to_s "last revised %B %e, %Y"
puts "number of elements: #{element_list.size}"
puts "highest element: #{element_list.last}"
Delphi
program Long_literals_with_continuations;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.StrUtils;
// Copy and past of Free_Pascal version
const
StdWordDelims: array[0..16] of char = (#0, ' ', ',', '.', ';', '/', '\', ':',
'''', '"', '`', '(', ')', '[', ']', '{', '}');
revisionNotice = 'Last update: %0:s';
elementString = 'hydrogen helium lithium beryllium boron carbon nitrogen oxy'
+ 'gen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chl'
+ 'orine argon potassium calcium scandium titanium vanadium chromium manganes'
+ 'e iron cobalt nickel copper zinc gallium germanium arsenic selenium bromin'
+ 'e krypton rubidium strontium yttrium zirconium niobium molybdenum techneti'
+ 'um ruthenium rhodium palladium silver cadmium indium tin antimony telluriu'
+ 'm iodine xenon cesium barium lanthanum cerium praseodymium neodymium prome'
+ 'thium samarium europium gadolinium terbium dysprosium holmium erbium thuli'
+ 'um ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium pla'
+ 'tinum gold mercury thallium lead bismuth polonium astatine radon francium '
+ 'radium actinium thorium protactinium uranium neptunium plutonium americium'
+ ' curium berkelium californium einsteinium fermium mendelevium nobelium law'
+ 'rencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmst'
+ 'adtium roentgenium copernicium nihonium flerovium moscovium livermorium te'
+ 'nnessine oganesson';
elementRevision = '2020-11-11';
begin
var words := elementString.Split(StdWordDelims);
writeLn(format(revisionNotice, [elementRevision]));
writeln(length(words));
writeln(words[high(words)]);
readln;
end.
- Output:
Last update: 2020-11-11 118 oganesson
Factor
The qw
vocabulary provides Perl-ish syntax for arrays of strings. For instance, the literal
qw{ a bc d }
expands to
{ "a" "bc" "d" }
during parse time. This is convenient to use when the strings that are stored contain no whitespace.
The convention in Factor is to limit lines to 64 characters wide if possible. This constraint is sometimes waived for large literals, but it was easy enough to accommodate here.
USING: formatting kernel qw sequences ;
qw{
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
}
"2020-03-23" ! last revision date in YYYY-MM-DD format
"Last revision: %s\n" printf
[ length ] [ last ] bi
"Number of elements: %d\nLast element: %s\n" printf
- Output:
Last revision: 2020-03-23 Number of elements: 118 Last element: oganesson
Free Pascal
The {$longStrings on} compiler directive enables the ANSI string data type. ANSI strings are pointers to sequences of characters. The data type includes a reference count and a (4-Byte long) length field.
program longStringLiteralDemo(output);
{$mode objFPC}
{$longStrings on}
uses
// for `format`
sysUtils,
// for `wordCount` and `extractWord`
strUtils;
const
elementString = 'hydrogen helium lithium beryllium boron carbon nitrogen oxy' +
'gen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chl' +
'orine argon potassium calcium scandium titanium vanadium chromium manganes' +
'e iron cobalt nickel copper zinc gallium germanium arsenic selenium bromin' +
'e krypton rubidium strontium yttrium zirconium niobium molybdenum techneti' +
'um ruthenium rhodium palladium silver cadmium indium tin antimony telluriu' +
'm iodine xenon cesium barium lanthanum cerium praseodymium neodymium prome' +
'thium samarium europium gadolinium terbium dysprosium holmium erbium thuli' +
'um ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium pla' +
'tinum gold mercury thallium lead bismuth polonium astatine radon francium ' +
'radium actinium thorium protactinium uranium neptunium plutonium americium' +
' curium berkelium californium einsteinium fermium mendelevium nobelium law' +
'rencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmst' +
'adtium roentgenium copernicium nihonium flerovium moscovium livermorium te' +
'nnessine oganesson';
elementRevision = '2020‑11‑11';
resourcestring
revisionNotice = 'Last update: %0:s';
begin
writeLn(format(revisionNotice, [elementRevision]));
writeLn(wordCount(elementString, stdWordDelims));
writeLn(extractWord(wordCount(elementString, stdWordDelims),
elementString, stdWordDelims));
end.
- Output:
Last update: 2020‑11‑11 118 oganesson
FreeBASIC
Dim As String ultimaRevision = "2021-11-12"
Dim As String elemento(0 to ...) => { _
"hydrogen", "helium", "lithium", "beryllium", "boron", "carbon", "nitrogen", _
"oxygen", "fluorine", "neon", "sodium", "magnesium", "aluminum", "silicon", _
"phosphorous", "sulfur", "chlorine", "argon", "potassium", "calcium", _
"scandium", "titanium", "vanadium", "chromium", "manganese", "iron", "cobalt", _
"nickel", "copper", "zinc", "gallium", "germanium", "arsenic", "selenium", _
"bromine", "krypton", "rubidium", "strontium", "yttrium", "zirconium", _
"niobium", "molybdenum", "technetium", "ruthenium", "rhodium", "palladium", _
"silver", "cadmium", "indium", "tin", "antimony", "tellurium", "iodine", _
"xenon", "cesium", "barium", "lanthanum", "cerium", "praseodymium", _
"neodymium", "promethium", "samarium", "europium", "gadolinium", "terbium", _
"dysprosium", "holmium", "erbium", "thulium", "ytterbium", "lutetium", _
"hafnium", "tantalum", "tungsten", "rhenium", "osmium", "iridium", "platinum", _
"gold", "mercury", "thallium", "lead", "bismuth", "polonium", "astatine", _
"radon", "francium", "radium", "actinium", "thorium", "protactinium", _
"uranium", "neptunium", "plutonium", "americium", "curium", "berkelium", _
"californium", "einsteinium", "fermium", "mendelevium", "nobelium", _
"lawrencium", "rutherfordium", "dubnium", "seaborgium", "bohrium", "hassium", _
"meitnerium", "darmstadtium", "roentgenium", "copernicium", "nihonium", _
"flerovium", "moscovium", "livermorium", "tennessine", "oganesson"}
Print "Last updated : "; ultimaRevision
Print "Number of elements : "; Ubound(elemento) - Lbound(elemento) + 1
Print "Last element : "; elemento(Ubound(elemento))
Sleep
- Output:
Last updated : 2021-11-12 Number of elements : 118 Last element : oganesson
Go
package main
import (
"fmt"
"regexp"
"strings"
)
// Uses a 'raw string literal' which is a character sequence enclosed in back quotes.
// Within the quotes any character (including new line) may appear except
// back quotes themselves.
var elements = `
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
`
func main() {
lastRevDate := "March 24th, 2020"
re := regexp.MustCompile(`\s+`) // split on one or more whitespace characters
els := re.Split(strings.TrimSpace(elements), -1)
numEls := len(els)
// Recombine as a single string with elements separated by a single space.
elements2 := strings.Join(els, " ")
// Required output.
fmt.Println("Last revision Date: ", lastRevDate)
fmt.Println("Number of elements: ", numEls)
// The compiler complains that 'elements2' is unused if we don't use
// something like this to get the last element rather than just els[numEls-1].
lix := strings.LastIndex(elements2, " ") // get index of last space
fmt.Println("Last element : ", elements2[lix+1:])
}
- Output:
Last revision Date: March 24th, 2020 Number of elements: 118 Last element : oganesson
Haskell
elements = words "hydrogen \
\ fluorine neon sodium magnesium \
\ aluminum silicon phosphorous sulfur \
\ chlorine argon potassium calcium \
\ scandium titanium vanadium chromium \
\ manganese iron cobalt nickel \
\ copper zinc gallium germanium \
\ arsenic selenium bromine krypton \
\ rubidium strontium yttrium zirconium \
\ niobium molybdenum technetium ruthenium \
\ rhodium palladium silver cadmium \
\ indium tin antimony tellurium \
\ iodine xenon cesium barium \
\ lanthanum cerium praseodymium neodymium \
\ promethium samarium europium gadolinium \
\ terbium dysprosium holmium erbium \
\ thulium ytterbium lutetium hafnium \
\ tantalum tungsten rhenium osmium \
\ iridium platinum gold mercury \
\ thallium lead bismuth polonium \
\ astatine radon francium radium \
\ actinium thorium protactinium uranium \
\ neptunium plutonium americium curium \
\ berkelium californium einsteinium fermium \
\ mendelevium nobelium lawrencium rutherfordium \
\ dubnium seaborgium bohrium hassium \
\ meitnerium darmstadtium roentgenium copernicium \
\ nihonium flerovium moscovium livermorium \
\ tennessine oganesson"
*Main> length elements 111 *Main> take 5 elements ["hydrogen","fluorine","neon","sodium","magnesium"] *Main> filter (('a' ==) . head) elements ["aluminum","argon","arsenic","antimony","astatine","actinium","americium"]
J
Words (;:) is the sequential machine configured for lexical analysis of j. In many cases, certainly this one, j words look like English words. Words cannot have an inverse because it discards leading and trailing spaces, losing spacing information. The obverse unites the boxed words with a single space separation, the best it can do. Which is just right for this task.
NB. create a multi-line literal elements =: CRLF -.~ noun define hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon potassium calcium scandium titanium vanadium chromium manganese iron cobalt nickel copper zinc gallium germanium arsenic selenium bromine krypton rubidium strontium yttrium zirconium niobium molybdenum technetium ruthenium rhodium palladium silver cadmium indium tin antimony tellurium iodine xenon cesium barium lanthanum cerium praseodymium neodymium promethium samarium europium gadolinium terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium platinum gold mercury thallium lead bismuth polonium astatine radon francium radium actinium thorium protactinium uranium neptunium plutonium americium curium berkelium californium einsteinium fermium mendelevium nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium roentgenium copernicium nihonium flerovium moscovium livermorium tennessine oganesson ) NB. same under words space_separated_elements =: ]&.:;: elements tally=: # ;: elements last_element=: _1 {:: ;: elements revision=: '2020-03-23' 'Last revision: ', revision 'Number of elements: ' , ": tally 'Last element: ', last_element 'first and last 30 characters:' 30 ;&({.&space_separated_elements) _30 Last revision: 2020-03-23 Number of elements: 118 Last element: oganesson first and last 30 characters: ┌──────────────────────────────┬──────────────────────────────┐ │hydrogen helium lithium beryll│vermorium tennessine oganesson│ └──────────────────────────────┴──────────────────────────────┘
Java
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
public final class LongLiteralsWithContinuations {
public static void main(String[] aArgs) {
ZoneId zoneID = ZoneId.of("Asia/Shanghai");
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneID);
String dateTime = zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a"));
List<String> elements = splitToList(ELEMENTS).stream().
filter( s -> ! splitToList(UNNAMED_ELEMENTS).contains(s) ).toList();
System.out.println("Last revision Date: " + dateTime + " " + zoneID);
System.out.println("Number of elements: " + elements.size());
System.out.println("Last element : " + elements.get(elements.size() - 1));
}
private static List<String> splitToList(String aText) {
String excessWhiteSpaceRemoved = aText.trim().replaceAll("\\s+", " ");
return Arrays.stream(excessWhiteSpaceRemoved.split(" ")).toList();
}
private static final String ELEMENTS = """
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
""";
private static final String UNNAMED_ELEMENTS = """
ununennium unquadnilium triunhexium penthextrium
penthexpentium septhexunium octenntrium ennennbium
""";
}
- Output:
Last revision Date: 2023-08-03 19:19:48 pm Asia/Shanghai Number of elements: 118 Last element : oganesson
jq
# FOR FUTURE EDITORS:
# To add chemical elements, modify the CHEMICAL_ELEMENTS function,
# ensuring that the date is updated properly and that there is at least one
# space between the element names after concatenation of the strings.
# Do not include any of the "unnamed" chemical element names such as ununennium.
def CHEMICAL_ELEMENTS:
{date: "Wed Jun 23 00:00:00 EDT 2021",
elements: (
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon "
+ "sodium magnesium aluminum silicon phosphorus sulfur chlorine argon potassium "
+ "calcium scandium titanium vanadium chromium manganese iron cobalt nickel copper "
+ "zinc gallium germanium arsenic selenium bromine krypton rubidium strontium "
+ "yttrium zirconium niobium molybdenum technetium ruthenium rhodium palladium "
+ "silver cadmium indium tin antimony tellurium iodine xenon cesium barium "
+ "lanthanum cerium praseodymium neodymium promethium samarium europium gadolinium "
+ "terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum "
+ "tungsten rhenium osmium iridium platinum gold mercury thallium lead bismuth "
+ "polonium astatine radon francium radium actinium thorium protactinium uranium "
+ "neptunium plutonium americium curium berkelium californium einsteinium fermium "
+ "mendelevium nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium "
+ "meitnerium darmstadtium roentgenium copernicium nihonium flerovium moscovium "
+ "livermorium tennessine oganesson"
) }
;
def chemical_elements_array:
CHEMICAL_ELEMENTS.elements
# remove leading and trailing whitespace
| sub("^ *";"") | sub(" *$";"")
# return a list after splitting using whitespace between words as a separator
| [splits("[ \t]+")] ;
def report:
chemical_elements_array as $a
| "List last revised: \(CHEMICAL_ELEMENTS.date)",
"Length of element list: \($a|length)",
"Last element in list: \($a[-1])";
report
Invocation: jq -nrf program.jq
- Output:
List last revised: Wed Jun 23 00:00:00 EDT 2021 Length of element list: 118 Last element in list: oganesson
Julia
The task does not as of the current revision mention lower versus upper case, but the below is corrected per a request anyway. The task does ask to comment on which column code may start. The start column for code does not matter to Julia, or to most modern computer language compilers, other than in some cases (not string data) Python.
using Dates
# FOR FUTURE EDITORS:
#
# Add to this list by adding more lines of text to this listing, placing the
# new words of text before the last """ below, with all entries separated by
# spaces.
#
const CHEMICAL_ELEMENTS = """
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon
sodium magnesium aluminum silicon phosphorus sulfur chlorine argon potassium
calcium scandium titanium vanadium chromium manganese iron cobalt nickel copper
zinc gallium germanium arsenic selenium bromine krypton rubidium strontium
yttrium zirconium niobium molybdenum technetium ruthenium rhodium palladium
silver cadmium indium tin antimony tellurium iodine xenon cesium barium
lanthanum cerium praseodymium neodymium promethium samarium europium gadolinium
terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum
tungsten rhenium osmium iridium platinum gold mercury thallium lead bismuth
polonium astatine radon francium radium actinium thorium protactinium uranium
neptunium plutonium americium curium berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium nihonium flerovium moscovium
livermorium tennessine oganesson
"""
#
# END OF ABOVE LISTING--DO NOT ADD ELEMENTS BELOW THIS LINE
#
const EXCLUDED = split(strip(
"ununennium unquadnilium triunhexium penthextrium penthexpentium " *
" septhexunium octenntrium ennennbium"), r"\s+")
function process_chemical_element_list(s = CHEMICAL_ELEMENTS)
# remove leading and trailing whitespace
s = strip(s)
# return a list after splitting using whitespace between words as a separator
return [element for element in split(s, r"\s+") if !(element in EXCLUDED)]
end
function report()
filedate = Dates.unix2datetime(mtime(@__FILE__))
element_list = process_chemical_element_list()
element_count = length(element_list)
last_element_in_list = element_list[end]
println("File last revised (formatted as dateTtime): ", filedate, " GMT")
println("Length of element list: ", element_count)
println("last element in list: ", last_element_in_list)
end
report()
- Output:
File last revised (formatted as dateTtime): 2020-03-24T02:48:55.421 GMT Length of element list: 118 last element in list: oganesson
Kotlin
Kotlin has raw string literals with """
. It does not (yet) have a native date/time library, so we use Java’s here.
import java.time.Instant
const val elementsChunk = """
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
"""
const val unamedElementsChunk = """
ununennium unquadnilium triunhexium penthextrium
penthexpentium septhexunium octenntrium ennennbium
"""
fun main() {
fun String.splitToList() = trim().split("\\s+".toRegex());
val elementsList =
elementsChunk.splitToList()
.filterNot(unamedElementsChunk.splitToList().toSet()::contains)
println("Last revision Date: ${Instant.now()}")
println("Number of elements: ${elementsList.size}")
println("Last element : ${elementsList.last()}")
println("The elements are : ${elementsList.joinToString(" ", limit = 5)}")
}
- Output:
Last revision Date: 2021-03-09T15:27:39.396Z Number of elements: 118 Last element : oganesson The elements are : hydrogen helium lithium beryllium boron ...
Lua
revised = "February 2, 2021"
-- the long literal string is delimited by double square brackets: [[...]]
-- each word must be separated by at least one whitespace character
-- additional whitespace may optionally be used to improve readability
-- (starting column does not matter, clause length is more than adequate)
longliteral = [[
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
]]
-- the task requires the "final list" as single-space-between string version
-- (a more idiomatic overall approach would be to directly split into a table)
finallist = longliteral:gsub("%s+"," ")
elements = {}
-- longliteral could be used here DIRECTLY instead of using finallist:
for name in finallist:gmatch("%w+") do elements[#elements+1]=name end
print("revised date: " .. revised)
print("# elements : " .. #elements)
print("last element: " .. elements[#elements])
-- then, if still required, produce a single-space-between string version:
--finallist = table.concat(elements," ")
- Output:
revised date: February 2, 2021 # elements : 118 last element: oganesson
Nim
Using concatenations
import strutils
const RevDate = "2021-02-05"
# We use the concatenation operator "&" to assemble the strings.
# This is done at compile time and so the result is a long literal.
const ElementString =
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine " &
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon " &
"potassium calcium scandium titanium vanadium chromium manganese iron " &
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine " &
"krypton rubidium strontium yttrium zirconium niobium molybdenum " &
"technetium ruthenium rhodium palladium silver cadmium indium tin " &
"antimony tellurium iodine xenon cesium barium lanthanum cerium " &
"praseodymium neodymium promethium samarium europium gadolinium terbium " &
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum " &
"tungsten rhenium osmium iridium platinum gold mercury thallium lead " &
"bismuth polonium astatine radon francium radium actinium thorium " &
"protactinium uranium neptunium plutonium americium curium berkelium " &
"californium einsteinium fermium mendelevium nobelium lawrencium " &
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium " &
"roentgenium copernicium nihonium flerovium moscovium livermorium " &
"tennessine oganesson"
when isMainModule:
const ElementList = ElementString.split()
echo "Last revision date: ", RevDate
echo "Number of elements: ", ElementList.len
echo "Last element in list: ", ElementList[^1]
- Output:
Last revision date: 2021-02-05 Number of elements: 118 Last element in list: oganesson
Using a long literal string
import strutils
const RevDate = "2021-02-05"
# We use a long string literal starting and ending with '"""'.
# We eliminate the multiple spaces by using "splitWhiteSpace"
# instead of "split".
const ElementString =
"""
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
"""
when isMainModule:
const ElementList = ElementString.splitWhitespace()
echo "Last revision date: ", RevDate
echo "Number of elements: ", ElementList.len
echo "Last element in list: ", ElementList[^1]
- Output:
Last revision date: 2021-02-05 Number of elements: 118 Last element in list: oganesson
Pascal
See #Free Pascal
The “Extended Pascal” standard (ISO 10206) defines the + string/character literal concatenation character. In “Unextended” Standard Pascal (ISO 7185) literals may not span multiple lines.
Perl
Mostly pointless...
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Long_literals,_with_continuations
use warnings;
my $longliteral = join ' ', split ' ', <<END;
hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine
neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon
potassium calcium scandium titanium vanadium chromium manganese iron cobalt
nickel copper zinc gallium germanium arsenic selenium bromine krypton rubidium
strontium yttrium zirconium niobium molybdenum technetium ruthenium rhodium
palladium silver cadmium indium tin antimony tellurium iodine xenon cesium
barium lanthanum cerium praseodymium neodymium promethium samarium europium
gadolinium terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium iridium platinum gold mercury thallium lead
bismuth polonium astatine radon francium radium actinium thorium protactinium
uranium neptunium plutonium americium curium berkelium californium einsteinium
fermium mendelevium nobelium lawrencium rutherfordium dubnium seaborgium
bohrium hassium meitnerium darmstadtium roentgenium copernicium nihonium
flerovium moscovium livermorium tennessine oganesson
END
my $version = 'Tue Feb 2 22:30:48 UTC 2021';
my $count = my @elements = split ' ', $longliteral;
my $last = $elements[-1];
print <<END;
version: $version
element count: $count
last element: $last
END
- Output:
version: Tue Feb 2 22:30:48 UTC 2021 element count: 118 last element: oganesson
Phix
Back-ticks and triple-quotes permit multi-line strings. We first replace all/any cr/lf/tab characters with spaces, then split (by default on a single space), omitting empty elements. You could use spaced_elements = join(elements) to join them back up into a space-separated single string, if that's really what you want, and you could then, like Go, use rfind(' ',spaced_elements) to re-extract the last one. You could also, like Julia, use get_file_date(command_line()[2]) instead of the hand-written last_updated constant. Phix code is free-format, indent things however you like, there is no specific maximum line length.
constant last_updated = "March 24th, 2020", elements_text = ` hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon potassium calcium scandium titanium vanadium chromium manganese iron cobalt nickel copper zinc gallium germanium arsenic selenium bromine krypton rubidium strontium yttrium zirconium niobium molybdenum technetium ruthenium rhodium palladium silver cadmium indium tin antimony tellurium iodine xenon cesium barium lanthanum cerium praseodymium neodymium promethium samarium europium gadolinium terbium dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium platinum gold mercury thallium lead bismuth polonium astatine radon francium radium actinium thorium protactinium uranium neptunium plutonium americium curium berkelium californium einsteinium fermium mendelevium nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium roentgenium copernicium nihonium flerovium moscovium livermorium tennessine oganesson `, elements = split(substitute_all(elements_text,{'\n','\r','\t'}," "),no_empty:=true), fmt = """ Last revision: %s Number of elements: %d The last of which is: "%s" """ printf(1,fmt,{last_updated,length(elements),elements[$]})
- Output:
Last revision: March 24th, 2020 Number of elements: 118 The last of which is: "oganesson"
PureBasic
elements$= "hydrogen helium lithium beryllium boron carbon " +
"nitrogen oxygen fluorine neon sodium magnesium " +
"aluminum silicon phosphorous sulfur chlorine argon " +
"potassium calcium scandium titanium vanadium chromium " +
"manganese iron cobalt nickel copper zinc " +
"gallium germanium arsenic selenium bromine krypton " +
"rubidium strontium yttrium zirconium niobium molybdenum " +
"technetium ruthenium rhodium palladium silver cadmium " +
"indium tin antimony tellurium iodine xenon " +
"cesium barium lanthanum cerium praseodymium neodymium " +
"promethium samarium europium gadolinium terbium dysprosium " +
"holmium erbium thulium ytterbium lutetium hafnium " +
"tantalum tungsten rhenium osmium iridium platinum " +
"gold mercury thallium lead bismuth polonium " +
"astatine radon francium radium actinium thorium " +
"protactinium uranium neptunium plutonium americium curium " +
"berkelium californium einsteinium fermium mendelevium nobelium " +
"lawrencium rutherfordium dubnium seaborgium bohrium hassium " +
"meitnerium darmstadtium roentgenium copernicium nihonium flerovium " +
"moscovium livermorium tennessine oganesson"
revision$= "2020-11-11"
Dim result$(0)
If OpenConsole() And CreateRegularExpression(0,"[a-z]{1,18}")
nbf=ExtractRegularExpression(0,elements$,result$())
PrintN("Last revision: "+revision$+
~"\nNumber of elements: "+Str(nbf)+
~"\nLast element: "+result$(nbf-1))
Input()
EndIf
- Output:
Last revision: 2020-11-11 Number of elements: 118 Last element: oganesson
Python
"""Long string literal. Requires Python 3.6+ for f-strings."""
revision = "October 13th 2020"
# String literal continuation. Notice the trailing space at the end of each
# line but the last, and the lack of commas. There is exactly one "blank"
# between each item in the list.
elements = (
"hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine "
"neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon "
"potassium calcium scandium titanium vanadium chromium manganese iron "
"cobalt nickel copper zinc gallium germanium arsenic selenium bromine "
"krypton rubidium strontium yttrium zirconium niobium molybdenum "
"technetium ruthenium rhodium palladium silver cadmium indium tin "
"antimony tellurium iodine xenon cesium barium lanthanum cerium "
"praseodymium neodymium promethium samarium europium gadolinium terbium "
"dysprosium holmium erbium thulium ytterbium lutetium hafnium tantalum "
"tungsten rhenium osmium iridium platinum gold mercury thallium lead "
"bismuth polonium astatine radon francium radium actinium thorium "
"protactinium uranium neptunium plutonium americium curium berkelium "
"californium einsteinium fermium mendelevium nobelium lawrencium "
"rutherfordium dubnium seaborgium bohrium hassium meitnerium darmstadtium "
"roentgenium copernicium nihonium flerovium moscovium livermorium "
"tennessine oganesson"
)
def report():
"""Write a summary report to stdout."""
items = elements.split()
print(f"Last revision date: {revision}")
print(f"Number of elements: {len(items)}")
print(f"Last element : {items[-1]}")
if __name__ == "__main__":
report()
- Output:
Last revision date: October 13th 2020 Number of elements: 118 Last element : oganesson
Quackery
[ stack ] is last-revision ( --> $ )
$ "6 Jun 2021" last-revision put
[ stack ] is elements ( --> $ )
$ " hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson"
nest$
$ "" swap
witheach [ join space join ]
-1 split drop
elements put
[ last-revision share decho$ ] is echorevision ( --> )
[ elements share nest$ size ] is elementcount ( --> n )
[ elements share
reverse nextword
nip reverse ] is finalelement ( --> $ )
say "Last revision: " echorevision cr
say "Number of elements: " elementcount echo cr
say "Last element: " finalelement echo$ cr
- Output:
Last revision: 6 Jun 2021 Number of elements: 118 Last element: oganesson
Raku
Incorrectly marked incorrect. Enforcing rules that exists only in somebodies head.
Not really sure I understand the point of this task. Seems to be load some list into memory and manipulate it somehow. Exceptionally boring to just read it in and then read it back out again. Perform some more interesting manipulations. Use < > quoting construct for literal string; unlimited (memory limited) characters, spaces don't matter, new-lines don't matter, blank lines don't matter.
my %periodic;
%periodic<revision-date> = Date.new(2020,3,23);
%periodic<table> = |<
Hydrogen 1.0079 H Helium 4.0026 He
Lithium 6.941 Li Beryllium 9.0122 Be
Boron 10.811 B Carbon 12.0107 C
Nitrogen 14.0067 N Oxygen 15.9994 O
Fluorine 18.9984 F Neon 20.1797 Ne
Sodium 22.9897 Na Magnesium 24.305 Mg
Aluminum 26.9815 Al Silicon 28.0855 Si
Phosphorus 30.9738 P Sulfur 32.065 S
Chlorine 35.453 Cl Potassium 39.0983 K
Argon 39.948 Ar Calcium 40.078 Ca
Scandium 44.9559 Sc Titanium 47.867 Ti
Vanadium 50.9415 V Chromium 51.9961 Cr
Manganese 54.938 Mn Iron 55.845 Fe
Nickel 58.6934 Ni Cobalt 58.9332 Co
Copper 63.546 Cu Zinc 65.39 Zn
Gallium 69.723 Ga Germanium 72.64 Ge
Arsenic 74.9216 As Selenium 78.96 Se
Bromine 79.904 Br Krypton 83.8 Kr
Rubidium 85.4678 Rb Strontium 87.62 Sr
Yttrium 88.9059 Y Zirconium 91.224 Zr
Niobium 92.9064 Nb Molybdenum 95.94 Mo
Technetium 98 Tc Ruthenium 101.07 Ru
Rhodium 102.9055 Rh Palladium 106.42 Pd
Silver 107.8682 Ag Cadmium 112.411 Cd
Indium 114.818 In Tin 118.71 Sn
Antimony 121.76 Sb Iodine 126.9045 I
Tellurium 127.6 Te Xenon 131.293 Xe
Cesium 132.9055 Cs Barium 137.327 Ba
Lanthanum 138.9055 La Cerium 140.116 Ce
Praseodymium 140.9077 Pr Neodymium 144.24 Nd
Promethium 145 Pm Samarium 150.36 Sm
Europium 151.964 Eu Gadolinium 157.25 Gd
Terbium 158.9253 Tb Dysprosium 162.5 Dy
Holmium 164.9303 Ho Erbium 167.259 Er
Thulium 168.9342 Tm Ytterbium 173.04 Yb
Lutetium 174.967 Lu Hafnium 178.49 Hf
Tantalum 180.9479 Ta Tungsten 183.84 W
Rhenium 186.207 Re Osmium 190.23 Os
Iridium 192.217 Ir Platinum 195.078 Pt
Gold 196.9665 Au Mercury 200.59 Hg
Thallium 204.3833 Tl Lead 207.2 Pb
Bismuth 208.9804 Bi Polonium 209 Po
Astatine 210 At Radon 222 Rn
Francium 223 Fr Radium 226 Ra
Actinium 227 Ac Protactinium 231.0359 Pa
Thorium 232.0381 Th Neptunium 237 Np
Uranium 238.0289 U Americium 243 Am
Plutonium 244 Pu Curium 247 Cm
Berkelium 247 Bk Californium 251 Cf
Einsteinium 252 Es Fermium 257 Fm
Mendelevium 258 Md Nobelium 259 No
Rutherfordium 261 Rf Lawrencium 262 Lr
Dubnium 262 Db Bohrium 264 Bh
Seaborgium 266 Sg Meitnerium 268 Mt
Roentgenium 272 Rg Hassium 277 Hs
Darmstadtium ??? Ds Copernicium ??? Cn
Nihonium ??? Nh Flerovium ??? Fl
Moscovium ??? Mc Livermorium ??? Lv
Tennessine ??? Ts Oganesson ??? Og
>.words.map: { (:name($^a), :weight($^b), :symbol($^c)).hash };
put 'Revision date: ', %periodic<revision-date>;
put 'Last element by position (nominally by weight): ', %periodic<table>.tail.<name>;
put 'Total number of elements: ', %periodic<table>.elems;
put 'Last element sorted by full name: ', %periodic<table>.sort( *.<name> ).tail.<name>;
put 'Longest element name: ', %periodic<table>.sort( *.<name>.chars ).tail.<name>;
put 'Shortest element name: ', %periodic<table>.sort( -*.<name>.chars ).tail.<name>;
put 'Symbols for elements whose name starts with "P": ', %periodic<table>.grep( *.<name>.starts-with('P') )».<symbol>;
put "Elements with molecular weight between 20 & 40:\n ",%periodic<table>.grep( {+.<weight> ~~ Numeric and 20 < .<weight> < 40} )».<name>;
put "SCRN: ", %periodic<table>[87,17,92]».<symbol>.join.tclc;
- Output:
Revision date: 2020-03-23 Last element by position (nominally by weight): Oganesson Total number of elements: 118 Last element sorted by full name: Zirconium Longest element name: Rutherfordium Shortest element name: Tin Symbols for elements whose name starts with "P": P K Pd Pr Pm Pt Po Pa Pu Elements with molecular weight between 20 & 40: Neon Sodium Magnesium Aluminum Silicon Phosphorus Sulfur Chlorine Potassium Argon SCRN: Raku
REXX
using continuations
This method will not work for some REXXes such as PC/REXX and Personal REXX as those two REXXes have a clause length limit of 1,024 bytes.
The space BIF is used to eliminate superfluous blanks from the list.
Most modern REXXes have no practical limit for a clause length.
/*REXX pgm illustrates how to code a list of words (named chemical elements */
/*──────────────────────── ordered by their atomic number) in a list format. */
$= 'hydrogen helium lithium beryllium boron carbon' ,
'nitrogen oxygen fluorine neon sodium magnesium' ,
'aluminum silicon phosphorous sulfur chlorine argon' ,
'potassium calcium scandium titanium vanadium chromium' ,
'manganese iron cobalt nickel copper zinc' ,
'gallium germanium arsenic selenium bromine krypton' ,
'rubidium strontium yttrium zirconium niobium molybdenum',
'technetium ruthenium rhodium palladium silver cadmium' ,
'indium tin antimony tellurium iodine xenon' ,
'cesium barium lanthanum cerium praseodymium neodymium' ,
'promethium samarium europium gadolinium terbium dysprosium',
'holmium erbium thulium ytterbium lutetium hafnium' ,
'tantalum tungsten rhenium osmium iridium platinum' ,
'gold mercury thallium lead bismuth polonium' ,
'astatine radon francium radium actinium thorium' ,
'protactinium uranium neptunium plutonium americium curium' ,
'berkelium californium einsteinium fermium mendelevium nobelium' ,
'lawrencium rutherfordium dubnium seaborgium bohrium hassium' ,
'meitnerium darmstadtium roentgenium copernicium nihonium flerovium' ,
'moscovium livermorium tennessine oganesson'
/* [↑] element list using continuation (commas).*/
updated= 'February 29th, 2020' /*date of the last revision of list.*/
say 'revision date of the list: ' updated /*show the date of the last update. */
elements= space($) /*elide excess blanks in the list*/
#= words(elements) /*the number of elements " " " */
say 'number of elements in the list: ' # /*show " " " " " " */
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/
- output when using the default input:
revision date of the list: February 29th, 2020 number of elements in the list: 118 the last element is: oganesson
using concatenations
Note that at least one REXX has a maximum width of any one line, whether or not it is continued.
PC/REXX and Personal REXX have a limit is 250 characters (which includes comments).
Also note that REXX comments are not totally ignored by the parser, they are
kept around for tracing and
for the invocation of the sourceline BIF.
The REXX version uses concatenation (also called abutment) to build the list.
/*REXX pgm illustrates how to code a list of words (named chemical elements */
/*──────────────────────── ordered by their atomic number) in a list format. */
$= 'hydrogen helium lithium beryllium boron carbon'
$=$ 'nitrogen oxygen fluorine neon sodium magnesium'
$=$ 'aluminum silicon phosphorous sulfur chlorine argon'
$=$ 'potassium calcium scandium titanium vanadium chromium'
$=$ 'manganese iron cobalt nickel copper zinc'
$=$ 'gallium germanium arsenic selenium bromine krypton'
$=$ 'rubidium strontium yttrium zirconium niobium molybdenum'
$=$ 'technetium ruthenium rhodium palladium silver cadmium'
$=$ 'indium tin antimony tellurium iodine xenon'
$=$ 'cesium barium lanthanum cerium praseodymium neodymium'
$=$ 'promethium samarium europium gadolinium terbium dysprosium'
$=$ 'holmium erbium thulium ytterbium lutetium hafnium'
$=$ 'tantalum tungsten rhenium osmium iridium platinum'
$=$ 'gold mercury thallium lead bismuth polonium'
$=$ 'astatine radon francium radium actinium thorium'
$=$ 'protactinium uranium neptunium plutonium americium curium'
$=$ 'berkelium californium einsteinium fermium mendelevium nobelium'
$=$ 'lawrencium rutherfordium dubnium seaborgium bohrium hassium'
$=$ 'meitnerium darmstadtium roentgenium copernicium nihonium flerovium'
$=$ 'moscovium livermorium tennessine oganesson'
/* [↑] element list using abutments*/
update= '29Feb2020' /*date of the last revision of list.*/
say 'revision date of the list: ' update /*show the date of the last update. */
elements= space($) /*elide excess blanks in the list*/
#= words(elements) /*the number of elements " " " */
say 'number of elements in the list: ' # /*show " " " " " " */
say 'the last element is: ' word($, #) /*stick a fork in it, we're all done*/
- output when using the default input:
revision date of the list: 29Feb2020 number of elements in the list: 118 the last element is: oganesson
RPL
≪ { "hydrogen" "helium" "lithium" "beryllium" "boron" "carbon" "nitrogen" "oxygen" "fluorine" "neon" "sodium" "magnesium" "aluminum" "silicon" "phosphorous" "sulfur" "chlorine" "argon" "potassium" "calcium" "scandium" "titanium" "vanadium" "chromium" "manganese" "iron" "cobalt" "nickel" "copper" "zinc" "gallium" "germanium" "arsenic" "selenium" "bromine" "krypton" "rubidium" "strontium" "yttrium" "zirconium" "niobium" "molybdenum" "technetium" "ruthenium" "rhodium" "palladium" "silver" "cadmium" "indium" "tin" "antimony" "tellurium" "iodine" "xenon" "cesium" "barium" "lanthanum" "cerium" "praseodymium" "neodymium" "promethium" "samarium" "europium" "gadolinium" "terbium" "dysprosium" "holmium" "erbium" "thulium" "ytterbium" "lutetium" "hafnium" "tantalum" "tungsten" "rhenium" "osmium" "iridium" "platinum" "gold" "mercury" "thallium" "lead" "bismuth" "polonium" "astatine" "radon" "francium" "radium" "actinium" "thorium" "protactinium" "uranium" "neptunium" "plutonium" "americium" "curium" "berkelium" "californium" "einsteinium" "fermium" "mendelevium" "nobelium" "lawrencium" "rutherfordium" "dubnium" "seaborgium" "bohrium" "hassium" "meitnerium" "darmstadtium" "roentgenium" "copernicium" "nihonium" "flerovium" "moscovium" "livermorium" "tennessine" "oganesson" } "23/02/05" → elements updated ≪ "Updated: " updated + "# items: " elements SIZE →STR + "Last : " elements DUP SIZE GET + ≫ ≫ 'ELMTS' STO
ELMTS
- Output:
3: "Updated: 23/02/05" 2: "# items: 118" 1: "Last : oganesson"
Ruby
elements = %w(
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson)
puts "Last mutation #{ File.mtime(__FILE__) }
number of elements: #{elements.size}
last element: #{elements.last}"
- Output:
Last mutation 2021-08-30 17:14:16 +0200 number of elements: 118 last element: oganesson
Standard ML
(* space in \ .. \ is ignored *)
val elements = "\
\hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine \
\neon sodium magnesium aluminum silicon phosphorous sulfur chlorine argon \
\potassium calcium scandium titanium vanadium chromium manganese iron cobalt \
\nickel copper zinc gallium germanium arsenic selenium bromine krypton \
\rubidium strontium yttrium zirconium niobium molybdenum technetium \
\ruthenium rhodium palladium silver cadmium indium tin antimony tellurium \
\iodine xenon cesium barium lanthanum cerium praseodymium neodymium \
\promethium samarium europium gadolinium terbium dysprosium holmium erbium \
\thulium ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium \
\platinum gold mercury thallium lead bismuth polonium astatine radon \
\francium radium actinium thorium protactinium uranium neptunium plutonium \
\americium curium berkelium californium einsteinium fermium mendelevium \
\nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium \
\meitnerium darmstadtium roentgenium copernicium nihonium flerovium \
\moscovium livermorium tennessine oganesson"
fun report lst = print ("\
\revision date:\t2021-02-25\n\
\element count:\t" ^ Int.toString (length lst) ^ "\n\
\last element:\t" ^ List.last lst ^ "\n")
val () = report (String.fields (fn c => c = #" ") elements)
- Output:
revision date: 2021-02-25 element count: 118 last element: oganesson
Tcl
Using the neatly formatted table from other languages (can be reformatted easily using the non-standard utility column with option -t).
set mtime "2021-05-10 16:11:50"
set elements {
hydrogen helium lithium beryllium
boron carbon nitrogen oxygen
fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur
chlorine argon potassium calcium
scandium titanium vanadium chromium
manganese iron cobalt nickel
copper zinc gallium germanium
arsenic selenium bromine krypton
rubidium strontium yttrium zirconium
niobium molybdenum technetium ruthenium
rhodium palladium silver cadmium
indium tin antimony tellurium
iodine xenon cesium barium
lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium
terbium dysprosium holmium erbium
thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium
iridium platinum gold mercury
thallium lead bismuth polonium
astatine radon francium radium
actinium thorium protactinium uranium
neptunium plutonium americium curium
berkelium californium einsteinium fermium
mendelevium nobelium lawrencium rutherfordium
dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium
nihonium flerovium moscovium livermorium
tennessine oganesson
}
set elements [list {*}$elements]; # Normalize string representation
puts "Last modification time: $mtime"
puts "Number of elements: [llength $elements]"
puts "Last element: [lindex $elements end]"
- Output:
Last modification time: 2021-05-10 16:11:50 Number of elements: 118 Last element: oganesson
UNIX Shell
These modern shells have arrays, which are easily initialized across multiple lines. While has line continuations with `\\` are available, the parentheses enclosing the array initializer are sufficient to extend it across as many lines as needed:
main() {
elements=(
hydrogen helium lithium beryllium boron carbon
nitrogen oxygen fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur chlorine argon
potassium calcium scandium titanium vanadium chromium
manganese iron cobalt nickel copper zinc
gallium germanium arsenic selenium bromine krypton
rubidium strontium yttrium zirconium niobium molybdenum
technetium ruthenium rhodium palladium silver cadmium
indium tin antimony tellurium iodine xenon
cesium barium lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium terbium dysprosium
holmium erbium thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium iridium platinum
gold mercury thallium lead bismuth polonium
astatine radon francium radium actinium thorium
protactinium uranium neptunium plutonium americium curium
berkelium californium einsteinium fermium mendelevium nobelium
lawrencium rutherfordium dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium nihonium flerovium
moscovium livermorium tennessine oganesson)
updated=2021-08-30
printf 'Last update: %s\n' "$updated"
printf 'Element count: %d\n' ${#elements[@]}
printf 'Latest element: %s\n' "${elements[-1]}"
}
main "$@"
- Output:
Wren
We use a 'raw' string for this task.
import "./pattern" for Pattern
var elementStr =
"""hydrogen helium lithium beryllium boron carbon
nitrogen oxygen fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur chlorine argon
potassium calcium scandium titanium vanadium chromium
manganese iron cobalt nickel copper zinc
gallium germanium arsenic selenium bromine krypton
rubidium strontium yttrium zirconium niobium molybdenum
technetium ruthenium rhodium palladium silver cadmium
indium tin antimony tellurium iodine xenon
cesium barium lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium terbium dysprosium
holmium erbium thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium iridium platinum
gold mercury thallium lead bismuth polonium
astatine radon francium radium actinium thorium
protactinium uranium neptunium plutonium americium curium
berkelium californium einsteinium fermium mendelevium nobelium
lawrencium rutherfordium dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium nihonium flerovium
moscovium livermorium tennessine oganesson"""
var p = Pattern.new("+1/s") // matches 1 or more whitespace characters
var elements = p.splitAll(elementStr) // get a list of elements
elementStr = elements.join(" ") // recombine using a single space as separator
var lastUpdate = "2023-12-17"
System.print("Last updated : %(lastUpdate)")
System.print("Number of elements : %(elements.count)")
System.print("Last element : %(elements[-1])")
- Output:
Last updated : 2023-12-17 Number of elements : 118 Last element : oganesson
zkl
This solution uses a "doc string", a chunk of text that the parser eats verbatim. It starts and ends with #<<<. If started with #<<<", a leading " is added to the text. The text is then parsed as one [long] line.
The string split method creats a list of items split at white space (by default). To turn that into one string with one space between each item, use: elements.concat(" ")
revisionDate:="2020-03-23";
elements:=
#<<<"
hydrogen helium lithium beryllium boron carbon
nitrogen oxygen fluorine neon sodium magnesium
aluminum silicon phosphorous sulfur chlorine argon
potassium calcium scandium titanium vanadium chromium
manganese iron cobalt nickel copper zinc
gallium germanium arsenic selenium bromine krypton
rubidium strontium yttrium zirconium niobium molybdenum
technetium ruthenium rhodium palladium silver cadmium
indium tin antimony tellurium iodine xenon
cesium barium lanthanum cerium praseodymium neodymium
promethium samarium europium gadolinium terbium dysprosium
holmium erbium thulium ytterbium lutetium hafnium
tantalum tungsten rhenium osmium iridium platinum
gold mercury thallium lead bismuth polonium
astatine radon francium radium actinium thorium
protactinium uranium neptunium plutonium americium curium
berkelium californium einsteinium fermium mendelevium nobelium
lawrencium rutherfordium dubnium seaborgium bohrium hassium
meitnerium darmstadtium roentgenium copernicium nihonium flerovium
moscovium livermorium tennessine oganesson"
.split();
#<<<
println("Revision date: ",revisionDate);
println(elements.len()," elements, the last being \"",elements[-1],"\"");
- Output:
Revision date: 2020-03-23 118 elements, the last being "oganesson"
- Programming Tasks
- Solutions by Programming Task
- 11l
- 6502 Assembly
- 68000 Assembly
- Arturo
- AWK
- BASIC256
- C++
- Crystal
- Delphi
- System.SysUtils
- System.StrUtils
- Factor
- Free Pascal
- FreeBASIC
- Go
- Haskell
- J
- Java
- Jq
- Julia
- Kotlin
- Lua
- Nim
- Pascal
- Perl
- Phix
- Phix/basics
- PureBasic
- Python
- Quackery
- Raku
- Raku examples needing attention
- Examples needing attention
- REXX
- RPL
- Ruby
- Standard ML
- Tcl
- UNIX Shell
- Wren
- Wren-pattern
- Zkl