I have made a temperature type in which a user should be able to enter a temperature in Celsius, Fahrenheit, or Kelvin. I am making a function that given an input such as "100 F" it will split the string, and create a temperature with degrees 100 & scale F. I need to be able to split it by the white space so I can pass them into my constructor to make the type. I am currently doing it by using subs but overlooked that if I pass a larger number it would not split it correctly. Is there a way to split by a space, or white space in user input.
I have to use a picture as I am logging in virtually to a machine and cannot copy paste.
I need them to be able to enter anywhere from absolute zero of the respective temperature to as high as they want it to be, for example 1000 F.
To split a string, you should use clojure.string/split. Great examples here:
https://clojuredocs.org/clojure.string/split
Related
I have a list of instructions in my program and they are activated by entering a string. There are a large number of possible instructions. You could call them commands if you like.
I already have a program that can successfully execute the instructions I've added so far.
For example, adding a person to the database would require the user to enter add "John" "Doe".
This would output to the screen Added John Doe to the database, ID#1234. The IDs are random.
I know how to add colors; in this output text, "John Doe" would be colored green.
What I'm wondering is, can I make it so that color changes as one types? Because I learned how to use some kind of keyboard mode change so that when I type a password, all characters are displayed as * of any color I desire, or even nothing displayed at all, through "display" of \0, and I know how to do that. I was wondering if the color could change before the complete string (extracted with std::getline(std::cin, str);) is typed.
The first reason I want the colors to change is because when there are so many commands, and I already have more complex commands than that, I want to provide a way for the user to be able to correct syntax mistakes before they press enter. Something like Windows PowerShell, perhaps, which was written in C#. I know that C# is a very different language than C++, but if C# can achieve something like that, I want to see if C++ can as well. My hope is that it doesn't require thousands of lines of application-specific code, especially considering that PowerShell is an actual application and not a simple terminal-run executable. And while PowerShell appears to be open-source, I don't understand C#. See the bottom for the second reason.
I have no idea if this is possible, but because similar manipulation of entered text is possible (as I said, I know how to add colors and also mask text as some other single character like *), I want to know if this is also possible.
Simple examples:
Firstly, the user should know when they have not entered a valid command, and when they have. I want the text to be in red until the letters entered so far consist of an actual keyword, like add. So, the text would be red until the second d is added, when it reverts to white, and if another letter is entered, it becomes red again.
Example 1: add "John Elias" "Doe"
After the space, the text after "add" should be red no matter what, unless the character after the space is a quotation mark. In order to tell the user that they have not terminated the string, the text beyond the (orange?) quotation mark should be orange. When the final quotation mark is entered, the entire content of the quotation marks (including the quotation marks) should be some other color (probably green?) to tell the user that they have successfully entered an argument. The same applies to any instances of quotation-mark arguments. Note that a space is allowed in a quotation argument.
Example 2: list-info 1234
In this command, it gets more complex. list is a separate command, so the text should be red until t is entered, and it turns white. But then it turns red again after that, until o is entered, and it turns white again. The numerical argument following it should be red if the entered character isn't a digit. If it is, it's still red, because the only valid IDs are 3- or 4-digit numbers. It should turn green(?) once a third digit is entered, and still stay green when another digit is entered. But if a fifth digit is entered (or another character for that matter), the number turns red again. Although this would better be implemented as returning an error if the entered number is invalid, I would still like to know if this can be done as well.
Example 3: add "John" "Elias" "Doe" "fourth-string"
Since there is an overloaded function that enables an explicit first-middle-last name to be stored as well, it should be ok if there is a third string. But if there is a fourth string added, then it should be in red no matter what because add cannot take more than 3 arguments.
My question is, are any of these things possible? And yes, I am aware that it is almost certainly better to just implement an error system, but my intention is to expand my coding ability, and that is the second reason, and coding an error system will not do that because I have already done that for every command.
For reference, I'm operating in Linux Ubuntu 18.04, I compile with g++, my code conforms to C++17, I use ANSI escape sequences for color, bold, etc., and for masking characters with something like * I use a pointer to a char array (passed by address as char**) and a C-style FILE* to reference the input stream stdin (because I haven't bothered to conform it to a typical C++ implementation yet, learning ways to advance my current skills is my priority at this point in time).
So I have some code that does essentially this:
REAL, DIMENSION(31) :: month_data
INTEGER :: no_days
no_days = get_no_days()
month_data = [fill array with some values]
WRITE(1000,*) (month_data(d), d=1,no_days)
So I have an array with values for each month, in a loop I fill the array with a certain number of values based on how many days there are in that month, then write out the results into a file.
It took me quite some time to wrap my head around the whole 'write out an array in one go' aspect of WRITE, but this seems to work.
However this way, it writes out the numbers in the array like this (example for January, so 31 values):
0.00000 10.0000 20.0000 30.0000 40.0000 50.0000 60.0000
70.0000 80.0000 90.0000 100.000 110.000 120.000 130.000
140.000 150.000 160.000 170.000 180.000 190.000 200.000
210.000 220.000 230.000 240.000 250.000 260.000 270.000
280.000 290.000 300.000
So it prefixes a lot of spaces (presumably to make columns line up even when there are larger values in the array), and it wraps lines to make it not exceed a certain width (I think 128 chars? not sure).
I don't really mind the extra spaces (although they inflate my file sizes considerably, so it would be nice to fix that too...) but the breaking-up-lines screws up my other tooling. I've tried reading several Fortran manuals, but while some of the mention 'output formatting', I have yet to find one that mentions newlines or columns.
So, how do I control how arrays are written out when using the syntax above in Fortran?
(also, while we're at it, how do I control the nr of decimal digits? I know these are all integer values so I'd like to leave out any decimals all together, but I can't change the data type to INTEGER in my code because of reasons).
You probably want something similar to
WRITE(1000,'(31(F6.0,1X))') (month_data(d), d=1,no_days)
Explanation:
The use of * as the format specification is called list directed I/O: it is easy to code, but you are giving away all control over the format to the processor. In order to control the format you need to provide explicit formatting, via a label to a FORMAT statement or via a character variable.
Use the F edit descriptor for real variables in decimal form. Their syntax is Fw.d, where w is the width of the field and d is the number of decimal places, including the decimal sign. F6.0 therefore means a field of 6 characters of width with no decimal places.
Spaces can be added with the X control edit descriptor.
Repetitions of edit descriptors can be indicated with the number of repetitions before a symbol.
Groups can be created with (...), and they can be repeated if preceded by a number of repetitions.
No more items are printed beyond the last provided variable, even if the format specifies how to print more items than the ones actually provided - so you can ask for 31 repetitions even if for some months you will only print data for 30 or 28 days.
Besides,
New lines could be added with the / control edit descriptor; e.g., if you wanted to print the data with 10 values per row, you could do
WRITE(1000,'(4(10(F6.0,:,1X),/))') (month_data(d), d=1,no_days)
Note the : control edit descriptor in this second example: it indicates that, if there are no more items to print, nothing else should be printed - not even spaces corresponding to control edit descriptors such as X or /. While it could have been used in the previous example, it is more relevant here, in order to ensure that, if no_days is a multiple of 10, there isn't an empty line after the 3 rows of data.
If you want to completely remove the decimal symbol, you would need to rather print the nearest integers using the nint intrinsic and the Iw (integer) descriptor:
WRITE(1000,'(31(I6,1X))') (nint(month_data(d)), d=1,no_days)
Generic drug names are sometimes formatted like this:
X-Y Tab 5-325 MG, where the drug of interest is X and the amount is 5. A different column has pre-extracted this as: 5 MG-325 M or 10 MG-325 depending on the amount of X.
Is there a way I can extract the amount that is associated with a MG? I am not sure if it's possible to use IS LIKE since the column is character format, and then converting the amount since there is a space between the number and MG.
the examples you are providing suggest that the pre-extracted string is a number space MG. If that's always true you can use the scan function to get the number part of your string.
amount = scan(pre-extracted,1,' ');
you can do this in a data step. Look up the scan function and you can see other options that may help you customize this further.
I have one input field that will represent a height in feet and inches. Users want an inout mask on the field as well, so when typed, it will separate the two halves of my 4 byte field with a slash; i.e. XX/XX.
The first XX is feet, and can basically be any number, although this height represents loading docks, so I suppose anything over 15 or 20 feet would be ridiculous.
The second XX are inches, and needs to be limited between 0 and 11. I thought I came up with what I needed with jquery masked input plug in, but i couldn't get it to allow a single digit in one of the XX columns, and I don't want to force the user to enter a leading 0.
I'm thinking this is probably pretty easy to knock out with reg ex...I'm sure someone here can prove me right!
I wrote a blog post about using regular expressions for validating number ranges. It's generally not recommended, but in this limited case perhaps it's okay.
If you assume that the first XX can be 0-30 (to be safe), you can use this:
(30|[0-2]\d)\|(0\d|11)
Debuggex Demo
Suppose, we have input strings in a form I have/had alot/none/zero money.
I would like to have a set of output strings as follows (example 1):
I have alot money
I have none money
I have zero money
I had alot money
I had none money
I had zero money
But then, real task here, is to be able to choose one or more, or none input substrings to ignore. So, the output strings would look like this:
I money
or
first example
or
I alot money
I none money
I zero money
or
I
or
money
I hope you got the point.
How can i do this in the way, friendliest to cpu cycles ?
Ok, to break the ice, this is what im Not willing to do, but considering until brighter ideas:
generate all the output strings (mentioned, example 1).
iterating through strings, i filter out ones that meet my criteria, replace unwanted substrings with "".
put the resulting string into final output array only if it is not already there.
Also, the answer to why do i care for cpu cycles, is simple : the longer this task takes, the longer it will block worker thread.
This simplest way is to find all the spaces and / character and put individual word into a two-level list, you then get a structure like this:
I
have, had
a lot, none, zero
money
.
Now you just loop thru the tree and generate a result string.
To push it to an overkill, you may also write a token parser instead of doing strchr.
How can i do this in the way, friendliest to cpu cycles ?
Why do you care?