Jump to content

Add column to array


Recommended Posts

Added a 3rd case:

#include <Array.au3>
Local $aArray1[10][10]
Local $aArray2[10][5]

For $i = 0 To UBound($aArray1)-1
    For $j = 0 To UBound($aArray1,2)-1
        $aArray1[$i][$j] = 1
        If $j < UBound($aArray2,2) Then
            $aArray2[$i][$j] = 2
        EndIf
    Next
Next

$aHoriz = $aArray1
$aVert = $aArray1
$aVert2 = $aArray1
ReDim $aHoriz[UBound($aHoriz)][UBound($aArray2,2)+UBound($aHoriz,2)]
For $i = 0 To UBound($aHoriz)-1
    For $j = UBound($aHoriz,2)-1 To UBound($aHoriz,2) - UBound($aArray2,2) Step -1
        $aHoriz[$i][$j] = $aArray2[$i][$j-UBound($aHoriz,2)+UBound($aArray2,2)]
    Next
Next
_ArrayDisplay($aHoriz)


ReDim $aVert[UBound($aVert)+UBound($aArray2)][UBound($aVert,2)]
For $i = UBound($aVert)-1 To UBound($aVert) - UBound($aArray2) Step -1
    For $j = 0 To UBound($aArray2,2)-1
        $aVert[$i][$j] = $aArray2[$i-UBound($aVert)+UBound($aArray2)][$j]
    Next
Next
_ArrayDisplay($aVert)

; fill in right to left...
ReDim $aVert2[UBound($aVert2)+UBound($aArray2)][UBound($aVert2,2)]
For $i = UBound($aVert2)-1 To UBound($aVert2) - UBound($aArray2) Step -1
    For $j = UBound($aVert2,2)-1 To UBound($aVert2,2)-UBound($aArray2,2) Step -1
        $aVert2[$i][$j] = $aArray2[$i-UBound($aVert2)+UBound($aArray2)][$j-UBound($aVert2,2)+UBound($aArray2,2)]
    Next
Next
_ArrayDisplay($aVert2)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

jdelaney

 

I'm using _ArraySort in my code, following the proper parameters from the help files, but cannot figure out why the following in occurring :

For numbers it is sorting just using the first digit of a number regardless +/-.

All my data is read into a 2D array from a CSV file and is properly populated. 

I get results similar to this:

17.92

19.35

-27.03

32.35

4.05

-5.09

60.10

7.11

ArraySort is straight forward in the documentation, but apparently I'm doing something wrong. 

Link to comment
Share on other sites

jdelaney

Would you examine the following code and perhaps tell me why the following is happening?

I have commented lines, to indicate what is being done or trying to do.
It all works like I was hoping it would with the following exception.
 
Column 4 in the final array has numbers in it (which is correct). Following your lead I used "Number" in the code so as to ensure I am dealing with numbers and not strings/text.  I also have a section where I delete "0"s and this works as well.
What I am finding is about 5 "0"s that show up in column 4 in the final array.  If I used Number in the code, why would this be happening? What am I missing?  Is there something else I should be doing?
 
;open statement into array
#include <Array.au3>
#include <File.au3>
 
Local $aArray1[200][5] ;statement array
Local $aArray2[200][15] ;log array
$csv = FileRead ("C:UsersDocumentsApril2014.csv")
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aArray1[$rows[0] + 1][5 + 1]
$aArray1[0][0] = $rows[0]
For $i = 1 to $rows[0]
    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1
  $aArray1[$i][$j] = $temp[$j] ;
  if $j = 4 then
 $aArray1 [$i] [$j] = Number($aArray1 [$i] [$j]) ;ensure values in column 4 are numbers and not strings/text
       endIf
Next
   Next
;look at output
 
 _ArraySort($aArray1, 0, 0, 0, 4)
_ArrayDisplay($aArray1)
 
;--------end of statement program
 
;read entire log file, but use only first two columns in array
;log is 15 columns, but only first two will be used
$csv = FileRead ("C:Americanabc.csv")
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aArray2[$rows[0] + 1][15 + 1]
$aArray2[0][0] = $rows[0]
For $i = 1 to $rows[0]
    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1
        $aArray2[$i][$j] = $temp[$j] ;
if $j = 0 then
 $aArray2 [$i] [$j] = number($aArray2 [$i] [$j])
 endIf
 Next
   Next
 ;next is to redim from 100x15 to 100x2 to use first two columns
 redim $aArray2 [200] [2]
 _ArraySort($aArray2, 0, 0, 0, 0)
 _ArrayDisplay($aArray2)
;------------- end of logfile program
 
;move to view combination of statement and logfile
$aVert2 = $aArray1
 
; essentially adding the log array and statement array togeter right hand justified.
ReDim $aVert2[uBound($aVert2)+UBound($aArray2)][uBound($aVert2,2)]
For $i = UBound($aVert2)-1 To UBound($aVert2) - UBound($aArray2) Step -1
    For $j = UBound($aVert2,2)-1 To UBound($aVert2,2)-UBound($aArray2,2) Step -1
        $aVert2[$i][$j] = $aArray2[$i-UBound($aVert2)+UBound($aArray2)][$j-UBound($aVert2,2)+UBound($aArray2,2)]
 Next
 Next
;remove any amounts equaling zero (0), retain positive and negative values
for $i = UBound($aVert2)-1 To UBound($aVert2) - UBound($aArray2) Step -1
  if $aVert2[$i][4] = 0 Then
_ArrayDelete($aVert2,$i)
 
   EndIf
Next
;finally sort the output on the numberical field, whereby amounts from statement and amounts from log will be together
 _ArraySort($aVert2, 0, 0, 0, 4) ;sort on col 4
 ;next is to display sorted array
 _ArrayDisplay($aVert2)
 
 
 
 
 
 

 

Link to comment
Share on other sites

Please stop hijacking this thread with unrelated issues. Open a new thread to discuss your issue because it's gotten away from adding a column to an array.

Also, please post all code inside a code box. When creating a post, click the blue A button to access the code box.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...