Functions and loops - arrays

I am trying to make the function _EncryptionProcess() take arrays 1 by 1 and process them. I realized you can't have functions inside For loops.
The location in which the arrays need to be taken is where $aArray is typed, the arrays are stored in this value. The other variable defines the key size and value.
;Cuts the input up into piece;
$VariableToBeCut = "12345678"
$aArray = StringRegExp($VariableToBeCut, ".{2}", 3)
MsgBox(0, "die", $aArray[0]) ; personal check to make sure array works
$DataToBeEncrypted=_EncryptionProcess($aArray, $keyvalue, $keysize, 1) ;$aArray needs to be where the different arrays are processed
MsgBox(0, "Encrypted data", $DataToBeEncrypted)

This is how you should process array elements.
;Cuts the input up into piece;
$VariableToBeCut = "12345678"
$aArray = StringRegExp($VariableToBeCut, ".{2}", 3)
ConsoleWrite("Array element 0: " & $aArray[0] & #LF) ; personal check to make sure array works
For $i = 0 To UBound($aArray)-1
$DataToBeEncrypted = _EncryptionProcess($aArray[$i], $keyvalue, $keysize, 1)
ConsoleWrite("Element " & $i & " : " & $aArray[$i] & " DataToBeEncrypted: " & $DataToBeEncrypted & #LF)
Next

Related

Cycle through variables incremented by a digit

I have an Access form where a bunch of sections are repeated, ex: Name, Age, Gender. The variable names for these text boxes are: Name1, Name2, ...; Age1, Age2, ...; etc.
I want to store all these values in an Array after a button is pressed. Instead of hardcoding all the variable names, I was wondering if there is a way to cycle through these in a for loop e.g.:
For i = 1 to 4
ArrayName(i) = ("Name" & i).value
ArrayAge(i) = ("Age" & i).value
ArrayGender(i) = ("Gender" & i).value
next i
I can get the string Name1 by ("Name" & i) but when I add .value after it, it doesn't seem to work.
I have also tried storing the variable name as a string into another array and then trying to use that array element to get the value, but that doesn't work either.
The error is because code is just building a string "Name1" and string does not have Value property. Consider:
Me.Controls("Name" & i).Value
or simplify with:
Me("Name" & i)
Value is default property so don't have to reference. However, the Me qualifier is important, otherwise the array will just populate with the control's name "Name1", not the control's value.
Arrays are 0-base by default which means the element index begins with 0 so iterating 1 to 4 to reference array elements will not provide correct results. Consider:
ArrayName(i - 1) = Me("Name" & i)
Otherwise, declare arrays as 1-base with Option Base 1 in module header.
Instead of 3 1-dimension arrays, a single 2-dimension array might serve.
Dim aryD(4, 3) As Variant, x As Integer, y As Integer
For x = 1 To 4
For y = 1 To 3
aryD(x - 1, y - 1) = Me(Choose(y, "Name", "Age", "Gender") & x)
Next
'verify array elements values
Debug.Print aryD(x - 1, 0) & " : " & aryD(x - 1, 1) & " : " & aryD(x - 1, 2)
Next

<< Operator Appending String to List Acting Strange - Ruby

I'm trying to append a string (made up of characters from another string iterated through a for loop) to an array. But for some reason when I output the items of the array in the end they're all off.
Here's my output for the code I have:
r
racecar
a
aceca
c
cec
e
c
a
r
Final list:
racecar
racecar
acecar
acecar
cecar
cecar
ecar
car
ar
r
I tried adding a empty string to the end of my statement like so,
list << string_current + ""
and it seems to fix the problem. Does anyone know why this is?
def longest_palindrome(s)
n = s.length
max_string = ""
max_string_current = ""
string_current = ""
list = []
counter = 0
for i in (0...n)
for j in (i...n)
string_current << s[j]
# puts "string_current: #{string_current}"
if is_palindrome(string_current)
counter += 1
puts string_current
list << string_current
end
end
string_current = ""
end
puts "Final list:"
list.each do |item|
puts "#{item}"
end
end
def is_palindrome(string)
for i in (0..string.length/2)
if string[i] != string[string.length-1-i]
return false
end
end
return true
end
longest_palindrome("racecar")
I thought that the items in my final list should be idential to those being put into the list during.
list << string_current
This:
string_current << s[j]
modifies the string in-place. That means that this:
list << string_current
can put a correct string into list and then that string can be modified later.
When you append your blank string:
list << string_current + ""
you're creating a brand new string (string_current + "") and then pushing that new string onto list so you're doing this:
tmp = string_current + ""
list << tmp
A couple simple ways around your problem would be explicitly dup'ing the string:
if is_palindrome(string_current)
counter += 1
puts string_current
list << string_current.dup # <--------------------
end
or using += instead of << on string_current:
for i in (0...n)
string_current = ''
for j in (i...n)
string_current += s[j]
You should also consider replacing your for loops (which are rare in Ruby) with methods from Enumerable. I don't want to go down a refactoring rabbit hole right now so I'll leave that as an exercise for the reader.
You could also have fixed it by
list << string_current.dup
but the real problem is your
string_current << s[j]
Try out (for instance in irb) the following example:
list=[]
str='ab'
list << str
str << 'x'
puts list
You will see that the list contains now 'abx', not 'ab'.
The reason is that the list contains an object reference (pointer) to the string, and when you do the str << 'x', you don't create a new object, but modify the existing one, and list hence sees the updated version.

How do you ACCESS Array Elements using Angelscript?

I am trying to create a simple inventory system for a game I am creating. The inventory needs 2 pieces of information for each item in the inventory, "item name" and "item description".
The information I read from the Angelscript website says to create
an array of string with 2 elements each.
Apparently this is done by using
string[] inventory(2);
What does this mean 2 elements?
How do I access those 2 elements inside the array?
The code below seems to work, but doesn't do what I expect it would do.
void Inventory(string item, string description) {
string[] inventory(2); // an array of string with 2 elements each.
inventory.insertLast(item); // item name
inventory.insertLast(description); //item description
print("ID:"+ inventory[1]);
print("ID:"+ inventory[2]);
print("ID:"+ inventory[3]);
}
Inventory("gold_key",item);
output
ID:
ID:gold_key
ID:Use this gold key to unlock a gold door.
I am not sure want you want to do but accessing array can be done using arr_name[index].
While index begin in 0.
For example:
{
string[] inventory(2);
inventory[0] = "aaa";
inventory[1] = "bbb";
cout << "inventory include" << inventory[0] << "and" << inventory[1] << endl;
}
Your code does the following:
Create a string array with two empty strings.
Adding two other elements to end the array with insertLast.
After that the 2nd, 3rd and 4th element are dereferenced and printed.
It does not do what you expect because:
1) It‘s not a string array with two items each, it‘s a string array with two items.
2) You add item and description at the end of the array, after the two empty strings.
3) The indexing starts at 0 for the first element.
If you really want a two dimensional array try the following:
array<array<string>> inventory;
array<string> elem = {item, description};
inventory.insertLast(elem);
cout << inventory[0][0] << „: “ << inventory[0][1] << endl;

How to do prime reading through a 2D array in asp pages

For my school project I am supposed to read values from HTML form which has 30 rows and 4 columns.
I've made a 2d array and made the reading like this (please ignore the validation parts):
Sub primeRead()
For i = 0 To 29
cDetails(i, 0) = Request.Form("UnitCode_" & rowID+1)
If cDetails(i,0) = " " Then
response.write("Hello")
Exit For
End If
cDetails(i, 1) = Request.Form("CP_" & rowID)
If cDetails(i, 1) = "15" Or "20" Then
'response.write("Credits points must be 15 or 20")
'Exit For
End If
cDetails(i, 2) = Request.Form("YS_" & rowID)
If cDetails(i, 2) = " " Then
response.write("Hello")
Exit For
End If
cDetails(i, 3) = Request.Form("UM_" & rowID)
If cDetails(i,3) = " " Then
response.write("Hello")
Exit For
End If
rowID = rowID + 1
i = i + 1
Next
End Sub
However, when I output the input taken from the form like this:
<%For x = 0 To 29%>
<tr>
<td> <%response.write(cDetails(x, 0))%><td/>
<td> <%response.write(cDetails(x, 1))%><td/>
<td> <%response.write(cDetails(x, 2))%><td/>
<td> <%response.write(cDetails(x, 3))%><td/>
<td> <%response.write(cDetails(x, 3))%><td/>
<tr/>
<%Next%>
it only shows up to 15 rows. Is there any limitations with 2D arrays VBScript or something else causing this undesirable output?
You're misunderstanding how For loops work. The loop variable is automatically. Incrementing the variable in the loop body on top of that like you do will cause the code to skip every second row. Remove the line i = i + 1 from the loop:
For i = 0 To 29
...
i = i + 1
Next
Also, the condition cDetails(i, 1) = "15" Or "20" will not work the way you seem to expect. It will check if the value cDetails(i, 1) equals "15" or if the string "20" is true. If you want to compare a variable with more than one value you must compare the variable with each value individually:
If cDetails(i, 1) = "15" Or cDetails(i, 1) = "20" Then
...
End If

How to check if a variable in one array exists in another and how to replace it in ruby?

I'm asking for input and then asking which words does the user want to redact. Then trying to print out the string with the words redacted.
puts "Input please"
text = gets.chomp
puts "What would you like redacted, sir? Type DONE when nothing else to redact"
redact = Array.new
answer = gets.chomp
until answer.downcase == "done"
redact << answer.downcase
answer = gets.chomp
end
words = text.split (" ")
words.each do |word|
if word.include? (# An array to redact)
# Replace words here
print "REDACTED "
else
print word + " "
end
end
Just solved it one way!!
words.each do |word|
if redact.include?(word)
#IF THE ARRAY REDACT INCLUDES VARIABLE WORD, PRINT REDACT
print "REDACTED "
else print word + " "
end
end
You can do something like this.
words.map {|word| redact.include?(word) ? 'REDACTED' : word}
This will iterate over each of the elements in your words array, and look to see if the element is in the redact array. If it is, it changes its value to 'REDACT', else it keeps the word the same.
Note that this will create a new array, so you will probably want to assign it to a new variable. If you want to edit the words array in place, you can use the map! function.
boolean ? true_path : false_path
is called a ternary, it is just short for
if boolean
true_path
else
false_path
end

Resources