compare foreign language strings in scilab - scilab
I am trying to compare the input string with the strings present in the doc. I am using strcmp for the purpose. These are non-English strings. When the input string is English language, the output is correct. But for any Kannada (non-English language) word the output is the same. I am trying to write a program to check if the word is present in the database. Please guide me in what could be the problem.
The calling function is as below:
str_kan = handles.InputBox.String;
res = strcmp('str_kan','s1.text')
if res == 1 then handles.InputBox.String = string( ' present')
abort
else
handles.InputBox.String = string( 'not present')
abort
end
The whole program is as below:
global s1
f=figure('figure_position',[400,50],'figure_size',[640,480],'auto_resize','on','background',[33],'figure_name','Graphic window number %d','dockable','off','infobar_visible','off','toolbar_visible','off','menubar_visible','off','default_axes','on','visible','off');
handles.dummy = 0;
handles.InputBox=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tunga','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.0929487,0.6568182,0.4647436,0.1795455],'Relief','default','SliderStep',[0.01,0.1],'String','Enter a Kannada Word','Style','edit','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','InputBox','Callback','')
handles.CheckDB=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.1025641,0.4636364,0.4567308,0.1204545],'Relief','default','SliderStep',[0.01,0.1],'String','Check','Style','pushbutton','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','CheckDB','Callback','CheckDB_callback(handles)')
f.visible = "on";
function CheckDB_callback(handles)
str_kan = handles.InputBox.String;
res = strcmp('str_kan','s1.text')
if res == 1 then handles.InputBox.String = string( ' present')
abort
else
handles.InputBox.String = string( 'not present')
abort
end
endfunction
Here is an example showing that, in Scilab, strcmp() does support UTF-8 extended characters:
--> strcmp(["aα" "aα" "aβ"], ["aα" "aβ" "aα"])
ans =
0. -1. 1.
The problem in the original posted code is the confusion between literal values as "Hello" and variables names as Hello="my text", as already noted by PTRK.
Related
Detect ANSI in QB45 in DOSBOX
I have been using DOSbox and ANSI with some success, but I want to detect ANSI installed. Ralf Brown description for detecting ANSI is: --------V-2F1A00----------------------------- INT 2F - DOS 4.0+ ANSI.SYS - INSTALLATION CHECK AX = 1A00h Return: AL = FFh if installed Notes: AVATAR.SYS also responds to this call documented for DOS 5+, but undocumented for DOS 4.x And my code to detect ANSI is: ' detect ansi InregsX.AX = &H1A00 CALL InterruptX(&H2F, InregsX, OutregsX) PRINT "AX="; HEX$(OutregsX.AX) IF (OutregsX.AX AND &HFF) = &HFF THEN Ansi.Installed = -1 ELSE Ansi.Installed = 0 END IF IF Ansi.Installed THEN PRINT "Ansi installed." ELSE PRINT "Ansi not installed." END IF which always displays "Ansi not installed." is there some other way to detect ANSI??
I think you might resolve making the try to use ANSI codes. The code below implements the function ansiOn% that returns 1 if an ansi string is written on the screen (used as console: see the code) as expected, otherwise returns 0. The method that the function implements is to clear the screen and write a string that contains: the first char different from A$, the backward ANSI sequence for 1 char - CSI 1 D - and the content of A$. After the string is written, if the upper left char on the screen is A$ then ANSI is enabled. DECLARE FUNCTION ansiOn% () ansi% = ansiOn% CLS PRINT "ANSI is"; IF ansi% = 0 THEN PRINT "n't"; END IF PRINT " enabled." FUNCTION ansiOn% CLS : A$ = "C" OPEN "CON" FOR OUTPUT AS #1 PRINT #1, CHR$(ASC(A$) - 1); CHR$(27); "[1D"; A$; CLOSE #1 IF CHR$(SCREEN(1, 1)) = A$ THEN ansiOn% = 1 ELSE ansiOn% = 0 END IF END FUNCTION I tried this code using QB45 in a DOS 6.22 VM and it works.
I wrote this code to detect ANSI: Cls Const A$ = "alpha" Const B$ = "beta" Open "CONS:" For Output As #1 Print #1, A$ Z$ = Chr$(27) + "[2J" ' ansi cls Print #1, Z$; B$; Close #1 For T = 1 To Len(B$) T$ = T$ + Chr$(Screen(1, T)) Next If T$ = B$ Then Print "ANSI detected."
Tracing cases of failure got
I'm trying to populate an integer variable from a character variable. If there is any error found I want to show the error message and trace all the possible cases for failure got. //Defining variable Define variable char_value as character no-undo initial "kk". Define variable int_value as integer no-undo. define variable ix as integer no-undo. Assign int_value = integer(char_value) no-error. IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES > 0 THEN DO: MESSAGE ERROR-STATUS:NUM-MESSAGES " errors occurred during conversion." SKIP "Do you want to view them?" VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO UPDATE view-errs AS LOGICAL. IF view-errs THEN DO ix = 1 TO ERROR-STATUS:NUM-MESSAGES: MESSAGE ERROR- STATUS:GET-NUMBER(ix) ERROR-STATUS:GET- MESSAGE(ix). END. END. There are two conditions which I want to know. What char value I gave so that no. Of error returns will be more than 1. How can I trace all the possible cases for failure got.
The built-in conversion routine does not do what you want it to do. So you will need to parse your input prior to attempting to convert it. Something like this: function isDigit returns logical ( input d as character ): if length( d ) = 1 then return ( index( "0123456789", d ) > 0 ). else return no. end. procedure checkInteger: define input parameter integerString as character no-undo. define output parameter errorList as character no-undo. define output parameter ok as logical no-undo. define variable i as integer no-undo. define variable n as integer no-undo. define variable c as character no-undo. ok = yes. n = length( integerString ). do i = 1 to n: c = substring( integerString, i, 1 ). if i = 1 and c = "-" then next. if isDigit( c ) = no then do: ok = no. errorList = errorList + substitute( "The character '&1' at offset &2 is not a valid integer value~n", c, i ). end. end. errorList = trim( errorList, "~n" ). // remove the trailing newline (if any) return. end. define variable ok as logical no-undo. define variable errorList as character no-undo. run checkInteger( "12x34y56z789", output errorList, output ok ). if ok = yes then message "string is a properly formed integer, go ahead and convert it". else message "string was not correctly formed, do not try to convert it" skip errorList view-as alert-box information . Note #1 If the input contains unprintable characters the errorList string will display it literally and it will look kind of funny. You could, of course, encode them to be more readable. Doing so is left as an exercise. Or another question. Note #2 This code makes no attempt to check that the string value will fit into an integer or an int64. That is also left as an exercise.
While you can make your parsing as complex as you like, I would just keep it simple and ensure the user is provided enough information, which in this case is the complete input value: def var cc as char initial "kk". def var ii as int. ii = integer( cc ). catch e as progress.lang.error: message quoter( cc, "'" ) e:getMessage(1) view-as alert-box. end catch.
How do you access name of a ProtoField after declaration?
How can I access the name property of a ProtoField after I declare it? For example, something along the lines of: myproto = Proto("myproto", "My Proto") myproto.fields.foo = ProtoField.int8("myproto.foo", "Foo", base.DEC) print(myproto.fields.foo.name) Where I get the output: Foo
An alternate method that's a bit more terse: local fieldString = tostring(field) local i, j = string.find(fieldString, ": .* myproto") print(string.sub(fieldString, i + 2, j - (1 + string.len("myproto"))) EDIT: Or an even simpler solution that works for any protocol: local fieldString = tostring(field) local i, j = string.find(fieldString, ": .* ") print(string.sub(fieldString, i + 2, j - 1)) Of course the 2nd method only works as long as there are no spaces in the field name. Since that's not necessarily always going to be the case, the 1st method is more robust. Here is the 1st method wrapped up in a function that ought to be usable by any dissector: -- The field is the field whose name you want to print. -- The proto is the name of the relevant protocol function printFieldName(field, protoStr) local fieldString = tostring(field) local i, j = string.find(fieldString, ": .* " .. protoStr) print(string.sub(fieldString, i + 2, j - (1 + string.len(protoStr))) end ... and here it is in use: printFieldName(myproto.fields.foo, "myproto") printFieldName(someproto.fields.bar, "someproto")
Ok, this is janky, and certainly not the 'right' way to do it, but it seems to work. I discovered this after looking at the output of print(tostring(myproto.fields.foo)) This seems to spit out the value of each of the members of ProtoField, but I couldn't figure out the correct way to access them. So, instead, I decided to parse the string. This function will return 'Foo', but could be adapted to return the other fields as well. function getname(field) --First, convert the field into a string --this is going to result in a long string with --a bunch of info we dont need local fieldString= tostring(field) -- fieldString looks like: -- ProtoField(188403): Foo myproto.foo base.DEC 0000000000000000 00000000 (null) --Split the string on '.' characters a,b=fieldString:match"([^.]*).(.*)" --Split the first half of the previous result (a) on ':' characters a,b=a:match"([^.]*):(.*)" --At this point, b will equal " Foo myproto" --and we want to strip out that abreviation "abvr" part --Count the number of times spaces occur in the string local spaceCount = select(2, string.gsub(b, " ", "")) --Declare a counter local counter = 0 --Declare the name we are going to return local constructedName = '' --Step though each word in (b) separated by spaces for word in b:gmatch("%w+") do --If we hav reached the last space, go ahead and return if counter == spaceCount-1 then return constructedName end --Add the current word to our name constructedName = constructedName .. word .. " " --Increment counter counter = counter+1 end end
How to display an inter object in the readline command in r?
I have declared an integer object called "teamStrength" and want to call it in the readline command. I know this is pretty much how to do it in Java but how do I display the object? "teamStrength" is supposed to change so I can't just display the actual number. teamStrength <- mean(teams$Fifa.Rating, trim = 0.5) CONTINUE <- readline(prompt = 'Your team strength is ' + teamStrength + 'Type yes if you want to continue. Type no if you want to rechoose. ')
The prompt string should be concatenated with paste(), rather than plus signs. Your code should be something like this: teamStrength <- mean(teams$Fifa.Rating, trim = 0.5) CONTINUE <- readline(prompt = paste('Your team strength is ', teamStrength, 'Type yes if you want to continue. Type no if you want to rechoose. '))
Python 3.4 help - using slicing to replace characters in a string
Say I have a string. "poop" I want to change "poop" to "peep". In fact, I also want all of the o's in poop to change to e's for any word I put in. Here's my attempt to do the above. def getword(): x = (input("Please enter a word.")) return x def main(): y = getword() for i in range (len(y)): if y[i] == "o": y = y[:i] + "e" print (y) main() As you can see, when you run it, it doesn't amount to what I want. Here is my expected output. Enter a word. >>> brother brether Something like this. I need to do it using slicing. I just don't know how. Please keep your answer simple, since I'm somewhat new to Python. Thanks!
This uses slicing (but keep in mind that slicing is not the best way to do it): def f(s): for x in range(len(s)): if s[x] == 'o': s = s[:x]+'e'+s[x+1:] return s
Strings in python are non-mutable, which means that you can't just swap out letters in a string, you would need to create a whole new string and concatenate letters on one-by-one def getword(): x = (input("Please enter a word.")) return x def main(): y = getword() output = '' for i in range(len(y)): if y[i] == "o": output = output + 'e' else: output = output + y[i] print(output) main() I'll help you this once, but you should know that stack overflow is not a homework help site. You should be figuring these things out on your own to get the full educational experience. EDIT Using slicing, I suppose you could do: def getword(): x = (input("Please enter a word.")) return x def main(): y = getword() output = '' # String variable to hold the output string. Starts empty slice_start = 0 # Keeps track of what we have already added to the output. Starts at 0 for i in range(len(y) - 1): # Scan through all but the last character if y[i] == "o": # If character is 'o' output = output + y[slice_start:i] + 'e' # then add all the previous characters to the output string, and an e character to replace the o slice_start = i + 1 # Increment the index to start the slice at to be the letter immediately after the 'o' output = output + y[slice_start:-1] # Add the rest of the characters to output string from the last occurrence of an 'o' to the end of the string if y[-1] == 'o': # We still haven't checked the last character, so check if its an 'o' output = output + 'e' # If it is, add an 'e' instead to output else: output = output + y[-1] # Otherwise just add the character as-is print(output) main() Comments should explain what is going on. I'm not sure if this is the most efficient or best way to do it (which really shouldn't matter, since slicing is a terribly inefficient way to do this anyways), just the first thing I hacked together that uses slicing. EDIT Yeah... Ourous's solution is much more elegant
Can slicing even be used in this situation?? The only probable solution I think would work, as MirekE stated, is y.replace("o","e").