What is the difference between \" and "" in Erlang - erlang

In Erlang, \" is an escape character that means double quote.
My question is, what is the difference between "\"test\"" and ""test""? The reason I ask is because, I'm trying to handle a list_to_atom error:
> list_to_atom("\"test\"").
'"test"'
> list_to_atom(""test"").
* 1: syntax error before: test

"" is a string/list of length 0
\" is just an escaped double-quote when used in the context of a string. If you wanted to have a string that consists of just a double-quote (ie \"), then you could do: "\"".
""test"" is a syntax error and is no difference than "" test "" which is syntactically <list><atom><list>. What are you trying to accomplish?

It's not advised to dynamically generate atoms as they're never garbage collected.
You'd better use list_to_existing_atom/1 when reading user input. Otherwise, you might end up running out of memory (in a system running long enough; but hey, that's what systems Erlang is for, isn't it?) and crashing the whole virtual machine.
list_to_existing_atom/1 will throw an error in case the atom doesn't exist and return the atom if it exists. A construct like catch list_to_existing_atom(some_atom) might prove useful coupled with a case .. of or a try ... catch block. Try it in the shell and see what you like the best.
If this answer seems irrelevant to the question, then please note I'm not allowed to post comments yet, so this answers the question in the comment to chops' answer, namely:
I have to write a function that reads from keyboard until an atom is typed.
I have to do this with get_line and list_to_atom. – otisonoza

Related

whitespace in flex patterns leads to "unrecognized rule"

The flex info manual provides allows whitespace in regular expressions using the "x" modifier in the (?r-s:pattern) form. It specifically offers a simple example (without whitespace)
(?:foo) same as (foo)
but the following program fails to compile with the error "unrecognized rule":
BAD (?:foo)
%%
{BAD} {}
I cannot find any form of (? that is acceptable as a rule pattern. Is the manual in error, or do I misunderstand?
The example in your question does not seem to reflect the question itself, since it shows neither the use of whitespace nor a x flag. So I'm going to assume that the pattern which is failing for you is something like
BAD (?x:two | lines |
of | words)
%%
{BAD} { }
And, indeed, that will not work. Although you can use extended format in a pattern, you can only use it in a definition if it doesn't contain a newline. The definition terminates at the last non-whitespace character on the definition line.
Anyway, definitions are overused. You could write the above as
%%
(?x:two | lines |
of | words ) { }
Which saves anyone reading your code from having to search for a definition.
I do understand that you might want to use a very long pattern in a rule, which is awkward, particularly if you want to use it twice. Regardless of the issue with newlines, this tends to run into problems with Flex's definition length limit (2047 characters). My approach has been to break the very long pattern into a series of definitions, and then define another symbol which concatenates the pieces.
Before v2.6, Flex did not chop whitespace off the end of the definition line, which also leads to mysterious "unrecognized rule" errors. The manual seems to still reflect the v2.5 behaviour:
The definition is taken to begin at the first non-whitespace character following the name and continuing to the end of the line.

lua: why the character "$" cause "unexpected symbol near $"

I am newbie in Lua. I write a script in which there is a line
ISH_activation.getState() = CONST_ACTIVATION_STATE$ON
Then, I need to write another C++ script to read this line from Lua and parse it. Now the problem is that, when I try to debug the lua script itself, at this line it gives me the error "unexpected symbol near '$'".
I don't know why this would happen. Is format like "CONST_ACTIVATION_STATE$ON" not allowed in Lua? And is "$" a special character in Lua that we cannot use it?(But I didn't find anything mentioning this)....
Well, did you find anything saying that you can use $? :)
Yes, you cannot use dollar signs in identifiers. From the manual:
Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit. Identifiers are used to name variables, table fields, and labels.
If you really want to keep the dollar sign you could use strings instead
"CONST_ACTIVATION_STATE$ON"

Rails strip all except numbers commas and decimal points

Hi I've been struggling with this for the last hour and am no closer. How exactly do I strip everything except numbers, commas and decimal points from a rails string? The closest I have so far is:-
rate = rate.gsub!(/[^0-9]/i, '')
This strips everything but the numbers. When I try add commas to the expression, everything is getting stripped. I got the aboves from somewhere else and as far as I can gather:
^ = not
Everything to the left of the comma gets replaced by what's in the '' on the right
No idea what the /i does
I'm very new to gsub. Does anyone know of a good tutorial on building expressions?
Thanks
Try:
rate = rate.gsub(/[^0-9,\.]/, '')
Basically, you know the ^ means not when inside the character class brackets [] which you are using, and then you can just add the comma to the list. The decimal needs to be escaped with a backslash because in regular expressions they are a special character that means "match anything".
Also, be aware of whether you are using gsub or gsub!
gsub! has the bang, so it edits the instance of the string you're passing in, rather than returning another one.
So if using gsub! it would be:
rate.gsub!(/[^0-9,\.]/, '')
And rate would be altered.
If you do not want to alter the original variable, then you can use the version without the bang (and assign it to a different var):
cleaned_rate = rate.gsub!(/[^0-9,\.]/, '')
I'd just google for tutorials. I haven't used one. Regexes are a LOT of time and trial and error (and table-flipping).
This is a cool tool to use with a mini cheat-sheet on it for ruby that allows you to quickly edit and test your expression:
http://rubular.com/
You can just add the comma and period in the square-bracketed expression:
rate.gsub(/[^0-9,.]/, '')
You don't need the i for case-insensitivity for numbers and symbols.
There's lots of info on regular expressions, regex, etc. Maybe search for those instead of gsub.
You can use this:
rate = rate.gsub!(/[^0-9\.\,]/g,'')
Also check this out to learn more about regular expressions:
http://www.regexr.com/

In the PowerShell grammar, what is the the `lvalueExpression` rule saying?

I was reviewing the PowerShell grammar posted here: http://www.manning.com/payette/AppCexcerpt.pdf
(I don't think it has been updated since PowerShell v1, and there are some typos. So, it's clearly not the true PowerShell Grammar, but a human-oriented document.)
In section C.2.1, it says:
<lvalueExpression> = <lvalue> [? |? <lvalue>]*
What is the meaning of the question marks? I can't tell if it means "match any character" or "match a question mark" or it's a typo.
I'm not sure what inputs this is intended to match, but maybe it's this:
$a,$b = 1, 2
in which case maybe the question mark is supposed to be a comma?
Based on its use in the preceding rule (<assignmentStatementRule> = <lvalueExpression> <AssignmentOperatorToken> <pipelineRule>), it appears that lvalueExpression in Appendix C of Windows PowerShell in Action corresponds to expression in section B.2.3 of The PowerShell Language Specification that Joey linked to. Matching it further than this is difficult, but I'll add some speculation anyway :)
The ? characters in [? |? <lvalue>]* are very likely erroneous. If it had been used to represent "the previous token is optional", then:
the [ and | tokens it was applied to should have been quoted
only [ makes sense as part of a value expression, but indexing is already covered later by the propertyOrArrayReferenceOperator rule
? is not used anywhere else in the grammar, but {0|1} is used multiple times to indicate "can appear zero or one times"
Given its similarity to [ '|' <cmdletCall> ]* at the end of the first rule in the section, it may have been a copy-and-paste error, compounded by a ‘smart quote’ round-trip encoding error. Assuming this was copied with the intent of editing later, then ?|? may have become '.' to represent multiple property accesses (but again, this is covered by the propertyOrArrayReferenceOperator rule).
Though based on the statement at the end of section C.2.1 that "[the pipeline rule] also handles parsing assignment expressions", lvalueExpression was probably intended to list all the assignable expressions besides simpleLvalue (e.g. cast-expression for [int]$x = 1, array-literal-expression for $a,$b,$c = 1,2,3), etc).

Bug in my regular expression

I'm trying to look at a string and reject anything that has seq= or app= in the string. Where it gets tricky is I need elements with q=something or p=something.
The seq= part of the string is always preceded an & and app= is always preceded by a ?
I have absolutely no idea where to start. I've been using http://www.rubular.com/ to try and figure it out but to no avail.
Any help would be hugely appreciated.
Based on your question, I believe you could just reject any strings that match the following expression:
[\?&](?:seq|app)=
This will match any string that contains a ? or & followed by either app= or seq=. The ?: inside the parentheses just tells the regular expression not to bother to capture matching groups as sub-matches. They're not really necessary, but what the heck.
Here's a Rubular link with some samples.

Resources