Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Multidimensional Array from .csv file
Old 10-23-2008, 10:34 AM Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Hello,
I have a csv list which appears like this:
00001;Product_1
00001;Product_2
00002;Product_3

I'm trying to assemble a script that will generate a Multi-dimensional Array from this csv file. I found something in PS-Script forum which, in part, suits my needs ("Compare filenames in a csv to files in a directory") and tried to modify it.

//---< Create Multi-Dimensional Array from CSV list >---
function main() {
//Ask the user to select the comma-delimited file
var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
//Make sure the comma-delimited file exists and then open it
datafile = new File(csvFile);
if (datafile.exists){
datafile.open('r');
};
var myRelazioni = new Array();
while(!datafile.eof){//read each line to end of file
myRelazioni.push(new Array(datafile.readln()));
}
datafile.close();
}
main();

This way I can get an Array which looks like this:
myRelazioni = [["00001;Product_1"],["00001;Product_2"],["00002;Product_3"]]
The question is that I want to obtain an Array which instead looks like this:
myRelazioni = [["00001;Product_1","00001;Product_2"],["00002;Product_3"]]
that is, I want to find a rule to group together those lines which start with the same set of characters into the same sub-array ("00001" in this case, but could be any set of characters).

Any suggestion would be much appreciated

Thanks
filippo nanni is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-23-2008, 12:11 PM Re: Multidimensional Array from .csv file
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
multidimensional arrays in JavaScript look like this:
Code:
var xo=[];
xo.push(new Array("hello world"));
alert(xo[0][0]);
will alert "hello world";
Then, you could add this:
Code:
xo[0].push("goodbye world");
alert(xo[0][1]);
will alert "goodbye world". I think you may see where this is going.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 10-23-2008 at 12:14 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-23-2008, 01:20 PM Re: Multidimensional Array from .csv file
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
javascript CANNOT open files on a users computer.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-23-2008, 06:19 PM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
As a graphic designer, I use Extend Script which is an extended version of Javascript (http://www.adobe.com/products/indesi...g/index.html); a scripting language which incorporates the benefits of Javascript together with the methods provided by the softwares that support such language (like Indesign). I'm aware that may be for a purist of Javascript, this may seem a little confusing, but in the end it's all about logic. So forgive me, but I was just trying to figure out how to solve a logical problem.
filippo nanni is offline
Reply With Quote
View Public Profile
 
Old 10-23-2008, 06:49 PM Re: Multidimensional Array from .csv file
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
So not javascript actually, but a scripting language with a javascript or C/C++ syntax. The same way as Action script (Flash) is.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 10-23-2008 at 06:50 PM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-23-2008, 07:05 PM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Exactly,
you can find more here http://wwwimages.adobe.com/www.adobe...ngGuide_JS.pdf, or here http://www.adobeforums.com/webx/.eea5b36/
As I told you before, I come from a graphic design background, not programmer - I'm moving my first steps into this world and it's hard to find good places to learn the language
filippo nanni is offline
Reply With Quote
View Public Profile
 
Old 10-24-2008, 03:36 AM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Do you think you could help me with my problem?..
filippo nanni is offline
Reply With Quote
View Public Profile
 
Old 10-24-2008, 05:39 AM Re: Multidimensional Array from .csv file
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
pushed for time today but I'll give you a couple of pointers to where I would be looking to go with code. Hopefully this will get you there. It will be fairly general as I don't know the details of Extend Script.

fill the array as you have done with each line of the file, then use arrayname.sort() to put it into ascending order. This should put the array into order with all the "000001" appearing first. It will of course rely on the format of the text file lines being the same.

then loop through the sorted array and concatenate the elements into a comma seperated string, when the first part of the element changes (000001: becomes 000002: etc) push the string into another array and clear the string.

this should leave you with
newArray[0] == "000001:Product1,000001:Product2,000001:Produc t3"
newArray[1] == "000002:Product1,000001:Product2"
newArray[2] == "000005:Product1,000001:Product2"
newArray[3] == "000009:Product1"

If I get time later I'll work out some code.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 10-24-2008 at 05:41 AM.. Reason: to break the smiley code
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-24-2008, 05:58 AM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Thank you chrishirst, this sounds promising.. I really appreciate your efforts.
filippo nanni is offline
Reply With Quote
View Public Profile
 
Old 10-25-2008, 12:19 PM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Hello chrishirst, have you got any news about this? I tried myself all day but it seems that my skills are not enough for this.. Hope to receive your answer
Thanks
filippo nanni is offline
Reply With Quote
View Public Profile
 
Old 10-25-2008, 10:35 PM Re: Multidimensional Array from .csv file
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
THis is javascript but provided the syntax is the same it should work
Code:
        <script type="text/javascript" >
	
	var arr2 = new Array;
	var arr1 = new Array;
	var newstring = "";
	var adding = ""
// load the lines from the external source here 

// defined array for testing
	arr1[0] = "000002:Product1";
	arr1[1] = "000001:Product1";
	arr1[2] = "000009:Product1";
	arr1[3] = "000001:Product3";
	arr1[4] = "000002:Product2";
	arr1[5] = "000005:Product1";
	arr1[6] = "000001:Product2";
	arr1[7] = "000005:Product2";
//	end of testing array

	
	arr1.sort();

	var s_start = arr1[0].substring(0,arr1[0].indexOf(":"))

	for (var i=0;i <= arr1.length-1; i++) {
	adding = arr1[i];
		if (i == 0) {
			newstring = adding;
		} else {			
			if (arr1[i].substring(0,arr1[i].indexOf(":")) == s_start) {
				newstring += "," + adding;
			} 
			if (arr1[i].substring(0,arr1[i].indexOf(":")) != s_start) {
				s_start = arr1[i].substring(0,arr1[i].indexOf(":"));
				arr2.push(newstring);
				newstring = arr1[i];
			}
		}
	} //end for 
			arr2.push(arr1[(arr1.length -1)]);
			// push last element onto array 2
					
		</script>
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-27-2008, 07:17 AM Re: Multidimensional Array from .csv file
Novice Talker

Posts: 9
Name: filippo
Trades: 0
Hello chrishirst,
I tried your script this morning and it worked perfect!
It is very well written so I was able to understand it as well. I thank you very much for your time and efforts also for proving me that Extend Script can support Javascript very well.
Thanks again
Filippo Nanni
filippo nanni is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multidimensional Array from .csv file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.43046 seconds with 12 queries