How to convert text lines to variables? - variables

Check out this list. I need each one turned into a variable and set equal to 0. Example:
;1-Methyoxy-2-Propanol would be:
$OneMethoxyTwoPropanol = 0
;and 1,2-BUTADIENE would be:
$OneTwoButadiene = 0
Assigning them to a variable wouldn't be a problem, but there are 1500 of them.

If i had to do this work i'll make it this way :
I'll change the case of each word :
Regex to change to sentence case
Make a "SearchAndReplace" :
1 -> One
2 -> Two
...
{underscore} -> ''
{space} -> ''
...
Then in a soft like SublimeText i'll add a $ in front of each line and a = 0 at the end. With the help of the Ctrl+Shift+L
Maybe you could use a regex to help you in the "SearchAndReplace" thing.

Something like this?
Local $sFilePath = #DesktopDir & "\test.ini"
Local $aArray = IniReadSection($sFilePath, "Variables")
Local $aVariablesArray[UBound($aArray)][2]
For $i = 1 To $aArray[0][0]
$aVariablesArray[$i][0] = $aArray[$i][0]
$aVariablesArray[$i][1] = $aArray[$i][1]
Next
For $i = 1 To UBound($aVariablesArray) -1
MsgBox(0, "", "Variable: " & $aVariablesArray[$i][0] & #CRLF & "Value: " & $aVariablesArray[$i][1])
Next
Your ini file should look like this
[Variables]
firstvariable=0
secondvariable=0
etc...=0
To create an ini file just open notepad, write down the file and then save it as .ini.
You can use RegEx to rename each file according to your needs.
The array created is a 2d array. It is needed to store the variable's name and value.

stringreplace and assign will do the job. If the amount of number to word replacements becomes too large you may consider storing those rather than nesting replace functions.
$sStr = "1-Methyoxy-2-Propanol" & #LF & "1,2-BUTADIENE"
$sStr = stringreplace(stringreplace(stringreplace(stringreplace($sStr , "," , "-") , "1-" , "One") , "2-" , "Two") , "-" , "")
$aStr = stringsplit($sStr , #LF , 2)
For $i = 0 to ubound($aStr) - 1
Assign($aStr[$i] , 0)
Next
msgbox(0, '' , Eval("OneMethyoxyTwoPropanol") & #LF & Eval("OneTwoBUTADIENE"))

I simply need to turn them into a variable and set them equal to 0.
As per Documentation - Intro - Arrays:
An Array is a variable containing a series of data elements. Each element in this variable can be accessed by an index number which relates to the position of the element within the Array - in AutoIt the first element of an Array is always element [0]. Arrays elements are stored in a defined order and can be sorted.
Removes lines containing list names (; List I etc.) and empty (or less than three character-) lines (as per $g_sRegexFilter). Stores remaining lines to 2D array-elements. Example:
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
Global Enum $ITEM_NAME, _
$ITEM_VALUE
Global Const $g_sFilePath = 'C:\list.txt'
Global Const $g_sFileNewline = #CRLF
Global Const $g_sRegexFilter = '(?m)^(.{0,2}\v)|(;.*\v)$'
Global Const $g_sItemHeader = 'name|value'
Global $g_sFileText = ''
Global $g_aFileItems
$g_sFileText = _TextFromFile($g_sFilePath)
$g_sFileText = StringRegExpReplace($g_sFileText, $g_sRegexFilter, '')
$g_aFileItems = StringSplit($g_sFileText, $g_sFileNewline, $STR_ENTIRESPLIT + $STR_NOCOUNT)
_ArrayColInsert($g_aFileItems, $ITEM_VALUE)
For $i1 = 0 To UBound($g_aFileItems) - 1
$g_aFileItems[$i1][$ITEM_VALUE] = 0
Next
_ArrayDisplay($g_aFileItems, '$g_aFileItems', '', 0, Default, $g_sItemHeader)
Func _TextFromFile(Const $sFile)
Local $hFile = FileOpen($sFile, $FO_READ + $FO_UTF8_NOBOM)
Local Const $sData = FileRead($hFile)
FileClose($hFile)
Return $sData
EndFunc
Returns:
1-Methoxy-2-Propanol | 0
1,2-BUTADIENE | 0
2-Diethyl aminoethanol | 0
2-ETHYL HEXANOL | 0
2-ETHYL HEXYL ACRYLATE | 0
2-Ethyl hexyl lights | 0
2-Ethyl phenol | 0
2-Ethylsuccionitrile | 0
2-Methyl piperidine | 0
2-Methyl-2-Butene nitrile | 0
2-Methyl-2-Pentenal | 0
2-Methyl-3-Butene nitrile | 0
2-Methylglutaronitrile | 0
...
As:
$g_aFileItems[ x ][$ITEM_NAME]
$g_aFileItems[ x ][$ITEM_VALUE]
Add additional columns using _ArrayColInsert().
... as there are 1500 of them.
Consider using SQLite. Related.

I am using Notepad++, so I'm not sure if this would work with any other IDEs/Notepads. I'm going to be using 1-Methoxy-2-Propanol for my following example.
I learned not to start variables with numbers, so I needed to replace them with words. 1-Methoxy-2-Propanol contains a 1 and a 2, we need to change these to One and Two.
Starting product:
1-Methoxy-2-Propanol
Press Ctrl + F and move to the replace tab. In the "Find what:" box, type 1. In the "Replace with:" box, type One, then press "replace all" (not just "replace"). Do this for numbers zero (0) through nine (9). Now, your product will look like this:
One-Methoxy-Two-Propanol
Next we need to get rid of the dashes. In the Replace tab, inside of the "Find what:" box, type - and in the "Replace With:" box, backspace completely so there is nothing there, then press "Replace All". Now, your product will look like this:
OneMethoxyTwoPropanol
There are other products that include comas and parenthesis, so simply find and replace these like above.
We need to add $ to the beginning of each word. Press Ctrl + F again and go to the Replace tab. In the "Find what:" box, type ^ which symbolizes the start of a new line. In the "Replace with:" box, type $ and press "Replace All". This will make your product look like:
$OneMethoxyTwoPropanol
We need to set all of these variables zero! Go back to the replace tab. In the "Find what:" box type \r. In the "Replace with:" box, type = 0. Note the space before the equal sign. Press "Replace All". Your product will look like this:
$OneMethoxyTwoPropanol = 0
Your file should have started like this:
1-Methoxy-2-Propanol
1,2-BUTADIENE
2-Diethyl aminoethanol
2-ETHYL HEXANOL
2-ETHYL HEXYL ACRYLATE
2-Ethyl hexyl lights
2-Ethyl phenol
2-Ethylsuccionitrile
2-Methyl piperidine
2-Methyl-2-Butene nitrile
2-Methyl-2-Pentenal
2-Methyl-3-Butene nitrile
2-Methylglutaronitrile
2-Pentene nitrile
2,4,7,9-Tetramethyl-5-decyne-4
And ended up like this:
$OneMethoxyTwoPropanol = 0
$OneTwoBUTADIENE = 0
$TwoDiethylaminoethanol = 0
$TwoETHYLHEXANOL = 0
$TwoETHYLHEXYLACRYLATE = 0
$TwoEthylhexyllights = 0
$TwoEthylphenol = 0
$TwoEthylsuccionitrile = 0
$TwoMethylpiperidine = 0
$TwoMethylTwoButenenitrile = 0
$TwoMethylTwoPentenal = 0
$TwoMethylThreeButenenitrile = 0
$TwoMethylglutaronitrile = 0
$TwoPentenenitrile = 0
$TwoFourSevenNineTetramethylFivedecyneFour = 0

Related

Dynamic variable assignment based on vector element values in Octave

I need to dynamically assign variable names to each row in a matrix based on the element values of the row so that I can later reference the appropriate row based on the variable rather than an index value.
I am having trouble identifying how to store the variable name along with its associated vector so that the vector can be later pulled up by simply typing in the variable name.
So far, using a simplified example I have:
A = [1 1 0; 1 0 1; 0 1 1];
s = cell(rows(A),1)
i = 1;
for i = 1:rows(A)
if (A(i,1) == 1 & A(i,2) == 1 & A(i,3) == 0) s(i,1) = struct("x1_y1_z0",{A(i,:)})
elseif (A(i,1) == 1 & A(i,2) == 0 & A(i,3) == 1) s(i,1) = struct("x1_y0_z1",{A(i,:)})
elseif (A(i,1) == 0 & A(i,2) == 1 & A(i,3) == 1) s(i,1) = struct("x0_y1_z1",{A(i,:)});
endif
i++
endfor
However, the resulting structue s lists x1_y1_z0 = 1 1 0 in a cell [1,1] and s.x1_y1_z0 does not return [1 1 0] as I would like.
Thanks very much for your guidance towards a working solution.

Gnuplot for loop with continous variables

I have a plot with many objects and labels. So I wanted to simplify the srcipt using loops. But I don't know how to adress the variables. I define the variables as followed
V1 = 10
V2 = 20
V3 = 23
...
LABEL1 = a
LABEL2 = b
...
The loop should look something like that
set for [i=1:15] label i at V(i),y_label LABEL(i)
This notation leads to errors compiling the script. Is it possiple at all to define such a loop in gnuplot? If so how can I do it?
Thanks for your help!
You can define a function which formats the label-definition as string and use a do loop to evaluate the strings:
y_label = 0
V1 = 10
V2 = 20
V3 = 23
LABEL1 = "a"
LABEL2 = "b"
LABEL3 = "c"
do for [i=1:3] {
eval(sprintf('set label %d at V%d,y_label LABEL%d', i, i, i))
}
Alternatively, you can use two string with whitespace-separated words for the iteration:
V = "10 20 23"
LABEL = "a b c"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
Note, that gnuplot 5.0 also has some limited support to use quote marks to hold several words together as one item:
V = "10 20 25"
LABEL = "'first label' 'second label' 'third one'"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)

Adding Data to an existing SeriesCollection in excel

I am creating a chart from data inside an Excel sheet. Everything works. But now I want to remove values that are below a limit and display them as "Others". Removing them works but I don't know how to add an additonal "others" value.
This is part of the code:
Co.chart.SetSourceData Source:=DataSource
Co.chart.ChartTitle.Text = "Best selling games"
Co.chart.SeriesCollection(1).ApplyDataLabels ShowPercentage:=True, ShowValue:=False
For Each d In Co.chart.SeriesCollection(1).DataLabels
v = CLng(Split(d.Caption, "%")(0))
If v < 10 Then
Rest = Rest + v
d.Delete
End If
Next
If Rest > 0 Then
Co.chart.SeriesCollection(1).DataLabels.AddData("Others",Rest); ' HERE
End If
In the second last line is some pseudocode about what I want to achieve.
I found a "dirty" solution for this. Instead of deleting the first item I RENAME it to "Others" instead of deleting it and adding the "Others" afterwards:
For Each d In Co.chart.SeriesCollection(1).DataLabels
Counter = Counter + 1
v = CLng(Split(d.Caption, "%")(0))
If v <= 10 Then
If RestPos < 0 Then
RestPos = Counter
Else
d.Delete
End If
Rest = Rest + v
End If
Next
If Rest > 0 Then
Co.chart.SeriesCollection(1).DataLabels(RestPos).Caption = Rest & " %"
End If

128 bit hex keygen

So my professor gave me a challenge to build a decoder that could break his special formula. It was described to be 32 characters in length, alphanumeric numeric when entered but then "it has a system... the first 106 bits must be 50% 1's and the rest 0's, the remaining 22 bits are basically a hash of the previous bits so that the key can be checked..." were his exact words. Sounds to me like a 128 bit encryption with a twist. I found the below but I need VB2010 or VS2010, this says php.
<?php
function string_random($characters, $length)
{
$string = '';
for ($max = mb_strlen($characters) - 1, $i = 0; $i < $length; ++ $i)
{
$string .= mb_substr($characters, mt_rand(0, $max), 1);
}
return $string;
}
// 128 bits is 16 bytes; 2 hex digits to represent each byte
$random_128_bit_hex = string_random('0123456789abcdef', 32);
// $random_128_bit_hex might be: '4374e7bb02ae5d5bc6d0d85af78aa2ce'
Would that work? Or does it need converting? Please help. Oh and thank you :)
I wasn't promised extra credit but either way I would like to surprise him.
So the first 106 bit are 26 character and the first half of the 27.
You have first of all encode somehow the number of 0 and 1, while building the string you need to keep an eye to the number. An idea would be to build a map like this:
0 = 0000 = -4
1 = 0001 = -2
2 = 0010 = -2
3 = 0011 = 0
4 = -2
5 = 0
6 = 0
7 = +2
8 = -2
9 = 0
a = 0
b = +2
c = 0
d = +2
e = +2
f = +4
then everytime you extract a new random number you check the number associated to it and add it to a variable
balanceOfOneAndZero
your objective is have balanceOfOneAndZero = 0 when you hit your 27th character.
to do that you need a control function, that takes current balanceOfOneAndZero, the proposed character proposedChar, and current string lenght currLenght.
Would be better to split the problem into two part. First is reaching the 26th character of the sequence with balanceOfOneAndZero between -2 and 2. Any other value is not acceptable, because your 27th character can have maximum two 1 or two 0 to completely balance the first 106 characters.
so your function should do something like (I'll write in sort of pseudo code since I don't have an IDE right now)
function checkNextLetter(Dim balanceOfOneAndZero As Integer, Dim proposedChar As Char,
Dim currentLenght as Integer) As Boolean
If( ((26 - currentLenght - 1) * 4 + 2) < MOD(Map.ValueOf(proposedChar) + balanceOfOneAndZero) ) Then
Return true
Else
Return false
ENd If
End function
This function basically check if accepting the new character will still make possible to Balance the number of 0 and 1 before the 26th character.
So your main function should have a loop every time it propose a new character, something like
proposedChar = new RandomChar
While (Not checkNextLetter(balanceOfOneAndZero, proposedChar, len(currentString))
proposedChar = new RandomChar
End While
currentString = currentString & proposedChar
this only until you hit the 26th character.
Than you have to check balanceOfOneAndZero, if its 2 you add a character that begin with 00, if it's 0 you can either have 10 or 01, if it's -2 you have to add a character that begin with 11.
After this I can't help you about the rest 22 character, since there are not enough information. You could brute force the rest
EDIT:
so to brute force the rest (il start from when you reach the 26th character):
Dim stringa1, stringa2, stringa3, stringa4 As String
If balanceOfOneAndZero = 2 Then
stringa1 = currentString & '0'
stringa2 = currentString & '1'
stringa3 = currentString & '2'
stringa4 = currentString & '3'
ELse If balanceOfOneAndZero = 0 Then
stringa1 = currentString & '4'
stringa2 = currentString & '5'
stringa3 = currentString & '6'
stringa4 = currentString & '7'
Else
stringa1 = currentString & 'c'
stringa2 = currentString & 'd'
stringa3 = currentString & 'e'
stringa4 = currentString & 'f'
End if
Function GenerateAllCombination(ByVal iLenght As Integer)
Dim arrayLista As New List(Of String)()
Dim arraySubLista As New List(Of String)()
If (iLenght > 1) Then
arraySubLista = GenerateAllCombination(iLenght -1)
for each objString As String in arraySubLista
for each ele As String in arrayValori
arrayLista.add(objString & ele)
loop
loop
Else
for each ele As String in arrayValori
arrayLista.add(ele)
loop
End If
End Function
Now if you use generateAllCombination you will have a List of string with ALL the combination of 5 character.
Now you just create 4 list by concatenating those combination with your string1 to string4 (string1 & combination) etc..
put all those result on a List of string, and you have 100% that at least ONE of the string will break your teacher code
I forgot, arrayValori must be a List with all values from "0" to "f"

Verify Gamefield VB.NET

So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. The problem is that when I'm verifying the columns and lines I need the program not to get out of the game field.
Here's how my code looks like now:
Public Sub avisinhos(ByVal line, ByVal column)
If mat(line, column) = 0 Then
mat(line, column) = -1
numbandeiras = numbandeiras + 1
End If
For auxlinha = -1 To 1
For auxcolumn = -1 To 1
Next
Next
End Sub
How do I create a IF function to verify that I don't get out of the game field?
Best regards, joao.
pseudo code
int linestart = -1;
int lineend = 1;
int colstart = -1;
int colend = 1;
Assuming a 10 x 10 grid (zero based)
if line < 2 linestart = 0
if line > 8 lineend = 0
if column < 2 colstart = 0
if column > 8 colend = 0
For auxlinha = linestart To lineend
For auxcolumn = colstart To colend
// check
Next
Next
Personally though I wouldn't bother with the loops, they add very little to nothing
HasMineAbove = (line > 1) and (gamefield[line -1,column] = MinePresentValue
would be my approach, do it all in one.
Not to mention the huge potential confusion when auxlinha and auxcolumn are both zero...
I'm not sure exactly what your code is saying. It's a bit cryptic since you're using abbreviations and all lowercase names. You might want to try camelCasing and spelling out the words more completely, intellisense is your friend. =)
But coding style aside, if you are trying to loop through a limited range of values, you can keep your values bounded by using the modulus operator (%). For example, if you need to keep you values between 0-7 and you end up with a value of 12, just take the modulus of 8 to loop back to within range with a value of 4:
12 % 8 = 4
9 % 8 = 1
15 % 8 = 7
24 % 8 = 0
I realize this doesn't answer your specific question, but it's a handy technique might find useful.