How to convert text lines to variables?
Asked Answered
O

5

1

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.

Overexert answered 16/3, 2016 at 23:42 Comment(0)
L
0

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.

Leola answered 17/3, 2016 at 13:22 Comment(0)
O
0

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.

Orient answered 17/3, 2016 at 18:13 Comment(1)
Alternative, but pretty much equivalent, methods would include using maps (a feature of some of the latest beta versions) or a Scripting.Dictionary object, instead of a 2d array.Boughten
O
0

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.

  1. 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
    
  2. 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
    
  3. 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
    
  4. There are other products that include comas and parenthesis, so simply find and replace these like above.

  5. 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
    
  6. 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
Overexert answered 18/3, 2016 at 23:32 Comment(0)
S
0

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.

Suggest answered 13/6, 2017 at 3:23 Comment(0)
C
0

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"))
Christo answered 3/3, 2018 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.