Jump to content
Atrax27

Ideas on how to create arrays easily?

Recommended Posts

7 minutes ago, Atrax27 said:

 

 think theres probably a way to count the number (12) without using the _FileCountLines function, but I can't easily see it. 

$_FileRead = FileReadToArray ($_FilePath)
Local $iCountLines = $_FileRead[0]

;$FRTA_NOCOUNT (0) - array is 0-based use UBound() to get size 
;$FRTA_COUNT (1) - array count in the first element. (default)<~~~~~~~~~~~~~~~~~~~~~~~~~~~~flags
;

read the function description it has everything you need to know about it .  note the flags 

Share this post


Link to post
Share on other sites

To simplify :

#include <Constants.au3>
#include <Array.au3>

$_FilePath = '1.txt'
$_FileRead = FileReadToArray ($_FilePath)
;_ArrayDisplay($_FileRead)
$Txt = "Dim $array[" & UBound($_FileRead) & "] = [" & '"' & _ArrayToString($_FileRead,CHR(34)&","&CHR(34)) & '"]'
ClipPut($Txt)
MsgBox($MB_SYSTEMMODAL, "", $Txt); may not need this but here just so I can quickly see what it's showing me

 

Share this post


Link to post
Share on other sites

Hey again,

Thanks for the "thanks" :) Since you didn't say if the macro helps you creating your data, can you use it? I just used my imagination for possible cases, of course that might not be the case.

If you would like to use it, does it need any modifications? Anything you want to be added/changed?

- Just noticed, you also have numbers, which may be formatted. Changing format of the Data sheet A column to text, and creating strData from range().text instead of range().value fixes that too. You get what you see this way. Uploading the modified xlsm.

And it should be creating the array you want, if you add that one line of AutoIt code. Then do whatever with the array. Why add extra steps to put commas and strip them away later?

image.png.3b8805f91f2ca5e5cc64ee9e783b2874.png

or when double quotes are prepended/appended

image.png.71691e960429dd2fca3520d4e8b949be.png

The test au3 is:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_type=a3x
#AutoIt3Wrapper_Outfile=file_read_to_array_test.a3x
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>


test()


func test()

    Local $aTest = FileReadToArray(@DesktopDir & "\array_list.txt")

    _ArrayDisplay($aTest,"Test")

EndFunc

 

test.xlsm

Share this post


Link to post
Share on other sites

Added a little functionality that greatly helps my particular workflow. I'm just so happy you guys

1) opens the "Array.txt" file

2) I paste my new list (directly from excel) into this text file

3) I save the file and close it

4) Boom, magic, done. 

 

I'm in love.

#include <Constants.au3>
#include <Array.au3>

Opt("WinTitleMatchMode",2)
ShellExecute("Notepad.exe", @scriptDir & "\ARRAY.txt")
Sleep(500)
WinWaitClose("ARRAY.txt")
$_FilePath = @scriptDir & "\ARRAY.txt"
$_FileRead = FileReadToArray ($_FilePath)
;_ArrayDisplay($_FileRead)
$Txt = "Dim $array[" & UBound($_FileRead) & "] = [" & '"' & _ArrayToString($_FileRead,CHR(34)&","&CHR(34)) & '"]'
ClipPut($Txt)
MsgBox($MB_SYSTEMMODAL, "", $Txt)

 

Edited by Atrax27

Share this post


Link to post
Share on other sites

Thanks GokAy, I think because I pull from so many different spreadsheets and different files, adding that data into an interim spreadsheet would just gum up everything, but I do appreciate your help thank you. 

Share this post


Link to post
Share on other sites

Fair enough, want to add though 😛

This can be done perhaps, to further streamline it.

Name or number of the spreadsheet that we copy the data doesn't matter. Instead of deleting and writing all data at once, everytime you select and copy them to data sheet, they are appended to your "array.txt", and Data sheet is cleaned.

Advantages would be; you control the order of data to be appended to "array.txt" (copy range1 from workbook1, manual copy/paste from other non-Excel file, copy range2 from workbook2, switch back to workbook1 and copy range3, etc). Spreadsheets would become trivial, and you can also copy/paste from your other non-Excel sources.

Just thinking here, what if I had to do the same task. Anyway, if you are happy, all is good. If you consider further tweaking let me know. :) We can manage whatever..

Share this post


Link to post
Share on other sites
10 hours ago, rudi said:

Some notes for my examples:

Some notes on my examples:

When you mark just one collumn in an Excel Spreadsheet and press CTRL+C, you will find in the clipboard all the values delimited by @CRLF.

 

Instead of my lines $s1d= <string-definition> you could also use

 

$s1D=ClipGet()

 

Same is fact for the example for a 2D array: When you mark a range of cells in Excel and copy it to your clipboard, the values of one line are @TAB delimited, the lines are delimeted by @CRLF

 

Same as above is fact for the $s2D string value: Instead of filling it manually with $s2D=<string-definition> you can use

$s2D=ClipGet()

... just after you've copied the marked cell range in Excel to your clipboard.

 

 

 

10 hours ago, rudi said:

expandcollapsepopup

#include <Array.au3>
#include <Debug.au3>


; 1D, just one collumn data

$s1D="Liam" & @crlf & _
"Olivia" & @crlf & _
"Noah" & @crlf & _
"Emma" & @crlf & _
"Oliver" & @crlf & _
"Ava" & @crlf & _
"William" & @crlf & _
"Sophia" & @crlf & _
"Elijah" & @crlf & _
"Isabella" & @crlf & _
"James" & @crlf & _
"Charlotte" & @crlf & _
"Benjamin" & @crlf & _
"Amelia" & @crlf & _
"Lucas" & @crlf & _
"Mia" & @crlf & _
"Mason" & @crlf & _
"Harper" & @crlf & _
"Ethan" & @crlf & _
"Evelyn"

$aWork=StringSplit($s1D,@CRLF,1)
_DebugArrayDisplay($aWork)




; 2D sample data from Excel
$s2D="XL Line1 Col1" & @TAB & "XL Line1 Col2" & @TAB & "XL Line1 Col3" & @CRLF & _
"XL Line2 Col1" & @TAB & "XL Line2 Col2" & @TAB & "XL Line2 Col3" & @CRLF & _
"XL Line3 Col1" & @TAB & "XL Line3 Col2" & @TAB & "XL Line3 Col3" & @CRLF & _
"XL Line4 Col1" & @TAB & "XL Line4 Col2" & @TAB & "XL Line4 Col3"



Dim $a2Work [1][3] ; Initial Array, correct number of col, "empty-line-0"
_ArrayAdd($a2Work,$s2D,0,@TAB,@CRLF)
$a2Work[0][0]=UBound($a2Work)-1

_DebugArrayDisplay($a2Work)

 

 


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...