Sign in to follow this  
Followers 0
huldu

CSV editing?

5 posts in this topic

Hi!

I was just wondering if someone knew a script that looks through a .csv file and creates columns and values from the .csv. Ive used the search function, of course, but it wouldnt let me search for a short word as "csv". Also tried search for string but that ended up with a gazillion results :)

Basically its a really simple script(i hope lol), that looks through a .csv like this:

DescID,TextDescription,Health,Loyalty,Endurance,Smarts,Attention,FOVRange,FOVAngle,EvidenceRating,Va

lueRating,ThreatRating,HeatGenerated,Armour.Health,Armour.Loyalty,Armour.Smarts,Armour.Attention,Arm

our.Endurance

1,A_MEGALOMANIAC_CRIMINAL_MASTERMIND,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80
2,A_LADY_MEGALOMANIAC_CRIMINAL_MASTERMIND,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80
3,DRAGON_VAN_DEN_DRIESSCHE,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80

The problem is i have no clue whatsoever how to get anything to work.

However i did find this code here which seems to work pretty well:

#include <array.au3>

$sString = 'DescID,TextDescription,Health,Loyalty,Endurance,Smarts,Attention,FOVRange,FOVAngle,EvidenceRatin

g,ValueRating,ThreatRating,HeatGenerated,Armour.Health,Armour.Loyalty,Armour.Smarts,Armour.Attention

,Armour.Endurance'

$a = _SplitStr($sString, ',')
_ArrayDisplay($a, '')

Func _SplitStr($sString, $vDelim)
    Local $aDQ = StringRegExp($sString, '".*?"', 3)
    If IsArray($aDQ) = 0 Then Return StringSplit($sString, $vDelim)
    For $iCC = 0 To UBound($aDQ) - 1
        $sString = StringReplace($sString, $aDQ[$iCC], 'SplitStr' & $iCC & 'Hold', 1, 1)
    Next
    Local $aSplit = StringSplit($sString, $vDelim)
    For $iCC = 1 To UBound($aSplit) - 1
        For $xCC = 0 To UBound($aDQ) - 1
            $aSplit[$iCC] = StringReplace($aSplit[$iCC], 'SplitStr' & $xCC & 'Hold', $aDQ[$xCC])
        Next
    Next
    Return $aSplit
EndFunc

But of course i wouldnt know how to script it to look for the first line in the .csv to create the columns, then look below for the "values"... complicated :)

I was hoping someone knew a script that did all this on this forum and could link it to me, please ^_^


"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites



The first regex will always return non array and thus return StringSpilt...

Can you give an example of the desired results?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The first regex will always return non array and thus return StringSpilt...

Can you give an example of the desired results?

Basically the goal would be to load the .csv file, the script would go through the file and create columns and add the values to them. The values would be saved in an array.

It would look something like this:

[DescID], [TextDescription], [Health], [Loyalty]....

1 A_MEGALOM.. 100 100

2 A_LADY_ME... 100 100

3 DRAGON_... 100 100

And so on. The goal would be to get the columns up and adding the values for each of the columns through all the classes.

The end result would be to have everything in a GUI, but that is something i can do. I would get all the column names so i can add them to the GUI, then have all the values added to each column.

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites

#include <array.au3>

Dim $sCSV = 'DescID,TextDescription,Health,Loyalty,Endurance,Smarts,Attention,FOVRange,FOVAngle,EvidenceRating,ValueRating,' & _
            'ThreatRating,HeatGenerated,Armour.Health,Armour.Loyalty,Armour.Smarts,Armour.Attention,Armour.Endurance' & _
            '1,A_MEGALOMANIAC_CRIMINAL_MASTERMIND,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80' & @LF & _
            '2,A_LADY_MEGALOMANIAC_CRIMINAL_MASTERMIND,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80' & @LF & _
            '3,DRAGON_VAN_DEN_DRIESSCHE,100,100,100,100,100,30,0.7,25,6,35,10,80,80,80,80,80'

Dim $aTmp = StringRegExp($sCSV, '\G(?i)([a-z]+\.?[a-z]*),?', 3)
Dim $aAtt[1]
Dim $aItems

If IsArray($aTmp) Then
    ReDim $aAtt[1][UBound($aTmp)+1]

    For $i = 0 To UBound($aTmp)-1
        $aAtt[0][$i] = $aTmp[$i]
    Next

    $aItems = StringRegExp($sCSV, '(?>\d.*)', 3)

    If IsArray($aItems) Then
        For $i = 0 To UBound($aItems)-1
            Local $ih = UBound($aAtt, 1)
            
            ReDim $aAtt[$ih+1][UBound($aAtt, 2)]
            
            Local $aItem = StringSplit($aItems[$i], ',', 2)
            For $j = 0 To UBound($aItem)-1
                $aAtt[$ih][$j] = $aItem[$j]
            Next
        Next                
    EndIf
EndIf

If IsArray($aAtt) Then _ArrayDisplay($aAtt)

Does the .csv file contain any line feed separators?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0