Jump to content

Easy array question


lampel
 Share

Recommended Posts

hello all , 

i am a begginer at autoit ,

i have a data text file (an array separated by "," delimiter) ,that i read to array using  _FileReadToArray

i cannot control this text file format  , This is a short version example  array (real one has 1000 cols, 100 rounds) :

I Have 12 team(rows) ,36 players ,each play 3 games each round

before_A.jpg

T=TEAM , P=PLAYER , S=SCORE

i want to transform this array from team array into Player array for easy calculation ,

This is how i want the new table will look like : 

AFTER_ARRAY_FIX.jpg

$playerNum=1
For $roundNumber=1 To 20
    For $TeamIndex=1 to 12
       $extract_Team=_ArrayExtract($TeamArray,$TeamIndex,$TeamIndex,1,700)

          For $index=1 To 3 
$PlayerArray[$playerNum][($roundNumber-1)*9+$index]=$extract_Team[0][($roundNumber-1)*9+$index]
          Next
          $playerNum+=1
     Next
          $playerNum=1
Next

this is what i got so far

i am trying for few hours and can't figure it out , 

thanks lampel

 

 

Edited by lampel
Link to comment
Share on other sites

11 minutes ago, AutoBert said:

Please post a link where i can read the rules of your competition , because with your 2 pictures i understand nothing:

sorry for that , 

i will try to clarify ,

its a bowling game , i need to manage player data like avg score , max game etc ...

in my league we have 12 teams each team with 3 players in it (36 players) , 

each round a player plays 3 games and his scores are saved ( ex = team1_player1 -- game1=100 game2=120 game3=140) 

this is arranged in the team array (1 pic) 

i want to transform that array (12 row=teams,1000 cols = players scores )

into a player array (36 rows=players , cols=player scores -- ex=player1 game1=100 game2=120 game3=140 game4=160....) ( this is the 2 pic)

Edited by lampel
Link to comment
Share on other sites

9 minutes ago, lampel said:

in my league we have 12 teams each team with 3 players in it (36 players) , 

and why i can see i pic 2 only 1 Team named T1 with 15 player named P1...P15 and 3 scores/round (scores is the only thing where pic and quote say the same) so

 

22 minutes ago, AutoBert said:

Please post a link where i can read the rules of your competition

i think the csv file could also help.

Link to comment
Share on other sites

2 hours ago, AutoBert said:

and why i can see i pic 2 only 1 Team named T1 with 15 player named P1...P15 and 3 scores/round (scores is the only thing where pic and quote say the same) so

you are correct! 

i uploaded the wrong pic2 ( i edited the post with the correct one) 

Link to comment
Share on other sites

Easy array question ? so so... :)
Using this file content

TEAM1,T1_P1_S1,T1_P1_S2,T1_P1_S3,T1_P2_S1,T1_P2_S2,T1_P2_S3,T1_P3_S1,T1_P3_S2,T1_P3_S3,T1_P1_S4,T1_P1_S5,T1_P1_S6,T1_P2_S4,T1_P2_S5,T1_P2_S6,T1_P3_S4,T1_P3_S5,T1_P3_S6,T1_P1_S7,T1_P1_S8,T1_P1_S9,T1_P2_S7,T1_P2_S8,T1_P2_S9,T1_P3_S7,T1_P3_S8,T1_P3_S9
TEAM2,T2_P1_S1,T2_P1_S2,T2_P1_S3,T2_P2_S1,T2_P2_S2,T2_P2_S3,T2_P3_S1,T2_P3_S2,T2_P3_S3,T2_P1_S4,T2_P1_S5,T2_P1_S6,T2_P2_S4,T2_P2_S5,T2_P2_S6,T2_P3_S4,T2_P3_S5,T2_P3_S6,T2_P1_S7,T2_P1_S8,T2_P1_S9,T2_P2_S7,T2_P2_S8,T2_P2_S9,T2_P3_S7,T2_P3_S8,T2_P3_S9

This should work

#include <File.au3>
#Include <Array.au3>

$file = @scriptdir & "\1.txt"

Local $a
_FileReadToArray($file, $a, 2, ",")  ; get an array of team arrays

$teams = UBound($a)   ; nb teams
$players = $teams*3   ; total nb players
$games = UBound($a[0])/3   ; nb games

Local $res[$players][$games]

For $i = 0 to $teams-1
     _ArraySort($a[$i])
   ; _ArrayDisplay($a[$i])   ; display each team array
    For $j = 0 to (3*$games)-1 step $games
        For $n = 0 to $games-1
            $res[($i*3)+$j/$games][$n] = ($a[$i])[$j+$n]
        Next
    Next
Next

 

Edited by mikell
Link to comment
Share on other sites

5 hours ago, mikell said:

Easy array question ? so so... :)
Using this file content

TEAM1,T1_P1_S1,T1_P1_S2,T1_P1_S3,T1_P2_S1,T1_P2_S2,T1_P2_S3,T1_P3_S1,T1_P3_S2,T1_P3_S3,T1_P1_S4,T1_P1_S5,T1_P1_S6,T1_P2_S4,T1_P2_S5,T1_P2_S6,T1_P3_S4,T1_P3_S5,T1_P3_S6,T1_P1_S7,T1_P1_S8,T1_P1_S9,T1_P2_S7,T1_P2_S8,T1_P2_S9,T1_P3_S7,T1_P3_S8,T1_P3_S9
TEAM2,T2_P1_S1,T2_P1_S2,T2_P1_S3,T2_P2_S1,T2_P2_S2,T2_P2_S3,T2_P3_S1,T2_P3_S2,T2_P3_S3,T2_P1_S4,T2_P1_S5,T2_P1_S6,T2_P2_S4,T2_P2_S5,T2_P2_S6,T2_P3_S4,T2_P3_S5,T2_P3_S6,T2_P1_S7,T2_P1_S8,T2_P1_S9,T2_P2_S7,T2_P2_S8,T2_P2_S9,T2_P3_S7,T2_P3_S8,T2_P3_S9

This should work

#include <File.au3>
#Include <Array.au3>

$file = @scriptdir & "\1.txt"

Local $a
_FileReadToArray($file, $a, 2, ",")  ; get an array of team arrays

$teams = UBound($a)   ; nb teams
$players = $teams*3   ; total nb players
$games = UBound($a[0])/3   ; nb games

Local $res[$players][$games]

For $i = 0 to $teams-1
     _ArraySort($a[$i])
   ; _ArrayDisplay($a[$i])   ; display each team array
    For $j = 0 to (3*$games)-1 step $games
        For $n = 0 to $games-1
            $res[($i*3)+$j/$games][$n] = ($a[$i])[$j+$n]
        Next
    Next
Next

 

mikell Thank you soo soo much !!

Thats a brilliant code!

after a small adaptation this code works fine. :)

can i ask for my  personal understanding,

why do you need to use the _ArraySort ? 

thanks lampel

Link to comment
Share on other sites

Did you try to compare by putting another _ArrayDisplay before the  _ArraySort ?  :)
Each team array has the same T  (T1, T2, etc) so the sorting is done for each element on the next value which is the P
Then all the P1, P2 etc are aggregated in order in the team array, it's necessary for the For/Next loops to work ...  ;)

Link to comment
Share on other sites

lampel,

How is the text file generated?

kylomas

edit: @mikell - Nice...I generated a test file file based on op's specs (12 teams x 3 players x 3 games x 100 rounds - 12X900 array) and ran your code against it.  I thought the multiple sorts would slow it down but run time is great.

; generate test file

#include <array.au3>

Local $line, $ret, $fl = @ScriptDir & '\bowlingtest.txt', $gameidx = 1
$hfl = FileOpen($fl, 2)
If $hfl = -1 Then Exit MsgBox(17, 'File Open Error', 'File = ' & $fl)

For $T = 1 To 12
    For $M = 1 To 100
        For $P = 1 To 3
            For $S = $gameidx To $gameidx + 2
                $line &= 'T' & StringFormat('%02s', $T) & '-P' & $P & '-S' & StringFormat('%03s', $S) & '-' & Random(150, 300, 1) & ','
            Next
        Next
        $gameidx += 3
    Next
    $ret = FileWrite($hfl, StringTrimRight($line, 1) & @CRLF)
    $line = ''
    $gameidx = 1
Next

FileClose($hfl)

; mikell's code

#include <File.au3>

Local $a
_FileReadToArray($fl, $a, 2, ",") ; get an array of team arrays

$teams = UBound($a) ; nb teams
$players = $teams * 3 ; total nb players
$games = UBound($a[0]) / 3 ; nb games

Local $res[$players][$games]

For $i = 0 To $teams - 1
    _ArraySort($a[$i])
    ; _ArrayDisplay($a[$i])   ; display each team array
    For $j = 0 To (3 * $games) - 1 Step $games
        For $n = 0 To $games - 1
            $res[($i * 3) + $j / $games][$n] = ($a[$i])[$j + $n]
        Next
    Next
Next

_ArrayDisplay($res)

 

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...