Suppose I have a string that takes the form "## ##"
in stdin
that I want to read in, e.g. "15 16"
. That's simple enough:
I HAS A STRINGY
GIMMEH STRINGY
That grabs the whole line from stdin
and stores it into the string—er, excuse me, YARN—called STRINGY
.
How would I go about extracting the two integers in the string? I want to be able to store 15 and 16 each as an integer (LULZ I MEANS NUMBR).
Short answer:
You can't do this in LOLCODE 1.2.
Long answer:
As there are no YARN
manipulation functions and no way to cast a YARN
to something like a BUKKIT
in the proposed 1.3 specification (whereupon which we could simulate a string as a BUKKIT
of characters), it's not possible to extract numbers from a string like that.
Using just the 1.2 spec, we can still make this work, I guess...
Since string concatenation already exists in 1.2 as SMOOSH ... MKAY
, you could, in theory, reserve variables str01
through str99
, and then...
IM IN YR readloop UPPIN YR throwawayvariable WILE BOTH OF NOT broken AN DIFFRINT currentyarn AN STRINGY
str01, WTF?
OMG "Z"
str99 R str98
str98 R str97
str97 R str96
...
str02 R str01
str01 R ""
GTFO
OMG "Y"
str01 R "Z";
GTFO
OMG "X"
str01 R "Y";
GTFO
...
OMG "0"
str01 R "1";
GTFO
OMG ""
str01 R "0"
GTFO
OMGWTF
I HAS A ERRORMSG
ERRORMSG R SMOOSH "I DUNNO WAT U SAY, illegal character", str01 MKAY
VISIBLE ERRORMSG
broken R WIN
GTFO
OIC
currentyarn R SMOOSH str01, str02, str03, str04, ..., str98, str99 MKAY
IM OUTTA YR readloop
Next, getting the NUMBRs...
NOT broken, O RLY? YA RLY
yarnnumber R ""
done R FAIL
IM IN YR parseloop UPPIN YR otherthrowaway WILE NOT done
EITHER OF BOTH SAEM str01 AN " " AN BOTH SAEM str01 AN "", O RLY? YA RLY
number1 R 0
DIFFRINT yarnnumber AN "", O RLY? YA RLY
number1 R MAEK yarnnumber A NUMBR
OIC
done R WIN
MEBBE DIFFRINT str01 AN ""
yarnnumber R SMOOSH yarnnumber, str01 MKAY
OIC
str01 R str02
str02 R str03
str03 R str04
...
str98 R str99
str99 R ""
IM OUTTA YR parseloop
yarnnumber R ""
done R FAIL
IM IN YR parseloop2 UPPIN YR otherthrowaway2 WILE NOT done
EITHER OF BOTH SAEM str01 AN " " AN BOTH SAEM str01 AN "", O RLY? YA RLY
number2 R 0
DIFFRINT yarnnumber AN "", O RLY? YA RLY
number2 R MAEK yarnnumber A NUMBR
OIC
done R WIN
MEBBE DIFFRINT str01 AN ""
yarnnumber R SMOOSH yarnnumber, str01 MKAY
OIC
str01 R str02
str02 R str03
str03 R str04
...
str98 R str99
str99 R ""
IM OUTTA YR parseloop2
OIC
And lastly, what ever you wanted to do with them.
I HAZ A result
result R SUM OF number1 AN number2
VISIBLE result
For ...
, fill in the remaining statements that make use of the str##
variables.
If you're gonna use this, I recommend using something to generate the LOLCODE for you, writing this by hand is just not feasible. Especially if you have to support longer input lines like 255 or 1024 characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With