Rails 3 to_i method susbtitute - ruby-on-rails-3

I have a string variable codigo = "0001" that I want to convert to an integer and increment its value by 1, I used to do this with codigo.to_i += 1 but apparently that method has been deprecated in Rails 3. Which is the Rails 3 way to do this now?

The basic problem here is that your variable, codigo, is a String.
codigo.to_i will return an integer, but it doesn't change the type of the variable it's called on, so it's still a String, you've just called a method on it that returns an integer.
codigo.to_i + 1 would return 2.
codigo.to_1 += 1 will produce an error because the return value isn't in any variable, so it can't be incremented.
So, to convert the variable to an integer and increment that, do this:
codigo = codigo.to_i
codigo += 1
If it makes more sense, you can also do this in one line:
codigo = codigo.to_i + 1

Related

How to convert Character Numerals into Numeric format?

I am trying to convert some String of Characters into Numeric form and then further incrementing it.
For Example : XX1-XXXXX.01.01.01.01 should be incremented to XX1-XXXXX.01.01.01.02 and if it reaches to XX1-XXXXX.01.01.01.99 then next increment should be XX1-XXXXX.01.01.02.00 and it continues for next node also.
XX1-XXXXX.01.01.01.01 is in Character Format.
Any Possible Solution?
This one is not compatible down to ABAP 700 like szakos but I want to bring in another solution:
FORM increment
CHANGING
fcw_out TYPE string.
DATA lw_as_n TYPE n LENGTH 8.
SPLIT fcw_out AT '.' INTO DATA(lw_start) DATA(lw_rest).
REPLACE ALL OCCURRENCES OF '.' IN lw_rest WITH ''.
lw_as_n = lw_rest + 1.
fcw_out = |{ lw_start }.{ lw_as_n(2) }.{ lw_as_n+2(2) }.{ lw_as_n+4(2) }.{ lw_as_n+6(2) }|.
ENDFORM.
" ------------------------------------------------
" Test run
" ------------------------------------------------
DATA(w_str) = `XX1-XXXXX.01.01.01.99`.
PERFORM increment CHANGING w_str. " ==> XX1-XXXXX.01.01.02.00
You can achieve this by converting the numerical parts of the node to a number and write it back after increasing. Simple example code:
DATA: l_node TYPE c LENGTH 21,
l_node_new TYPE c LENGTH 21,
l_help TYPE c LENGTH 11,
l_num TYPE n LENGTH 8,
l_node_pos TYPE int4,
l_num_pos TYPE int4.
l_node_new = l_node = 'XX1-XXXXX.01.01.01.01'.
l_help = l_node+10(*).
REPLACE ALL OCCURRENCES OF '.' IN l_help WITH ''.
CONDENSE l_help.
l_num = l_help.
DO 100 TIMES.
ADD 1 TO l_num.
ENDDO.
DO 4 TIMES.
l_node_pos = 10 + ( sy-index - 1 ) * 3.
l_num_pos = 0 + ( sy-index - 1 ) * 2.
l_node_new+l_node_pos(2) = l_num+l_num_pos(2).
ENDDO.
WRITE / l_node.
WRITE / l_node_new.
The output is:
The ABAP version was 700 so this is the "old" syntax, but should work on newer versions.

Possible to store a value in variable in SPSS?

Is is possible in SPSS to store a value in a variable (not a variable created in a data set)?
For example I have a loop for which I want to pass the value 4 to all the locations in the loop that say NumLvl.
NumLvl = 4.
VECTOR A1L(NumLvl-1).
LOOP #i = 1 to NumLvl-1.
COMPUTE A1L(#i) = 0.
IF(att1 = #i) A1L(#i) = 1.
IF(att1 = NumLvl) A1L(#i) = -1.
END LOOP.
EXECUTE.
You can do this using DEFINE / !ENDDEFINE SPSSs Macro Facility, for example:
DEFINE !MyVar () 4 !ENDDEFINE.
You can then use !MyVar as a substitute for 4 wherever in your syntax you wish.
See DEFINE / !ENDDEFINE documentation for further notes.

Octave keyboard input function to filter concatenated string and integer?

if we write 12wkd3, how to choose/filter 123 as integer in octave?
example in octave:
A = input("A?\n")
A?
12wkd3
A = 123
while 12wkd3 is user keyboard input and A = 123 is the expected answer.
assuming that the general form you're looking for is taking an arbitrary string from the user input, removing anything non-numeric, and storing the result it as an integer:
A = input("A? /n",'s');
A = int32(str2num(A(isdigit(A))));
example output:
A?
324bhtk.p89u34
A = 3248934
to clarify what's written above:
in the input statement, the 's' argument causes the answer to get stored as a string, otherwise it's evaluated by Octave first. most inputs would produce errors, others may be interpreted as functions or variables.
isdigit(A) produces a logical array of values for A with a 1 for any character that is a 0-9 number, and 0 otherwise.
isdigit('a1 3 b.') = [0 1 0 1 0 0 0]
A(isdigit(A)) will produce a substring from A using only those values corresponding to a 1 in the logical array above.
A(isdigit(A)) = 13
that still returns a string, so you need to convert it into a number using str2num(). that, however, outputs a double precision number. so finally to get it to be an integer you can use int32()

Substring function does not work in Vb?

I am trying to mask SSn and want show it on label caption.
lblSPTINTo.Caption = rsMM("SPTIN")
lblCPTINTo.Caption = rsMM("CPTIN")
i am trying to use substring function to get last 4 characters but i not am to able to use it as it throws compile error .
lblSPTINTo.Caption = rsMM("SPTIN").sutbstring(4,4)
Replace sutbstring with Substring.
But it won't work that way because the first parameter is the index and the second parameter in Substring is the length, if you want the last 4 characters:
Dim last4 As String = rsMM("SPTIN")
If last4.Length > 4 Then last4 = last4.Substring(last4.Length - 4)

Increment an integer

Sometimes ABAP drives me crazy with really simple tasks such as incrementing an integer within a loop...
Here's my try:
METHOD test.
DATA lv_id TYPE integer.
lv_id = 1.
LOOP AT x ASSIGNING <y>.
lv_id = lv_id+1.
ENDLOOP.
ENDMETHOD.
This results in the error message Field type "I" does not permit subfield access.
You already answered the question yourself, but to make things a bit clearer:
variable + 1
is an arithmetic expression - add 1 to the value of the variable.
variable+1
is an offset operation on a character variable. For example, if variable contains ABC, variable+1 is BC.
This can be especially confusing when dealing with NUMCs. For example, with variable = '4711', variable + 1 is evaluated to 4712, whereas variable+1 is '711' (a character sequence).
The error you saw occurred because it's not possible to perform the index operation on a non-character-like variable.
You mean like:
ADD 1 to lv_id.
By the way, when you loop over an internal table, SY-TABIX has the loop counter.
Uh, I got it.
It's the f****** spaces...
lv_id = lv_id + 1
works...
Simple
DATA : gv_inc type I .
place this statement in loop
gv_inc = gv_inc + 1 .
from SAP NetWeaver Version 7.54 you can also use:
lv_id += 1.
Instead of
lv_id = lv_id + 1.
Happy coding!
If you are going to increment every loop cycle than you can directly get the table size.
describe table x lines data(lv_id). "Out side of the loop.