perl Win32 OLE Excel

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
Guessing.... :D
I am guessing too on this $j=$k++; :)
Some perlmonk taught me that push join and splitting stuff and another 1 liner to output an array to a csv file. It works everytime. :s8:

Code:
push @array [split ',',[COLOR="Cyan"]join(',',@row)[/COLOR]];

If this is the case, only the highlighted part would have suffice to join the fields for a record into a line string. By calling split again, it would chop them back into pieces and pushed into @array.

Probably when we inspect some sample value of @row, then we will know why we need to join and split. Could be that some elements of @row already have commas (',') in them, and the split is to ensure those are all cut properly. The danger is that some fields may indeed hold valid values that should include commas and the simplistic splitting here would cause corruptions.

Recommended way of dealing with proper parsing and formatting of CSV is to use Text::CSV in CPAN.
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,754
Reaction score
533
Code:
push @array [split ',',[COLOR="Cyan"]join(',',@row)[/COLOR]];

If this is the case, only the highlighted part would have suffice to join the fields for a record into a line string. By calling split again, it would chop them back into pieces and pushed into @array.

Probably when we inspect some sample value of @row, then we will know why we need to join and split. Could be that some elements of @row already have commas (',') in them, and the split is to ensure those are all cut properly. The danger is that some fields may indeed hold valid values that should include commas and the simplistic splitting here would cause corruptions.

Recommended way of dealing with proper parsing and formatting of CSV is to use Text::CSV in CPAN.

Er.... don't want get arse burn for showing the data. The descriptive fields do not contain comma and mostly number fields. 18 fields in total and a few hundred K lines.

Maybe you or david can shed some light on this one. I have a subroutine.
process_data(\@arr1,\@arr2);

process_data {
my (@arr1, @rr2)= @_;

In this setup, the first call to the routine returned garbage data. 2nd call was OK. If no 2nd call, first call is OK.

Only when did this to get to work.
process_data(\@arr1,\@arr2);

process_data {
my ($ref1, $ref2)= @_;

my @arr1 = @{ref1};
my @arr2 = @{ref2};
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,754
Reaction score
533
Do show us the article you have read that does what you have suggested?

For your case of printing a CSV line for each row of data contained in an array, I would have done it this way.

Code:
{ local $,='","'; print '"', @row, '"', "\n" }

To ensure escaping are done properly, I would have done it more robustly this way,
Code:
{ local $,='","'; print '"', map { s/"/""/g } @row, '"', "\n" }

I believe this will be a single liner too, would it not be? :)

Sorry, only can locate this one.
http://www.perlmonks.org/?node_id=46529
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Er.... don't want get arse burn for showing the data. The descriptive fields do not contain comma and mostly number fields. 18 fields in total and a few hundred K lines.

Maybe you or david can shed some light on this one. I have a subroutine.
process_data(\@arr1,\@arr2);

process_data {
my (@arr1, @rr2)= @_;

In this setup, the first call to the routine returned garbage data. 2nd call was OK.

Only when did this to get to work.
process_data(\@arr1,\@arr2);

process_data {
my ($ref1, $ref2)= @_;

my @arr1 = @{ref1};
my @arr2 = @{ref2};

Your parameters expected an array, but what you pass in is a reference to an array.

dereference your $ref1 or $ref2 is the proper way.

if you want to retain the $ref1, use it this way
Code:
$ref1->[IDX]

Read this
https://gfx.github.io/perl.js/#$a = [2,3,4]; print $a->[1], "\n"; print @{$a}[1], "\n";
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302

Well I don't see where your case is similar to what is suggested in the article you have shared. In any case, it would seems very likely the way you using split and join in the attempt to create one complete multirows CSV to be rather incorrect.

If you really want to have one complete attempt to create a complete CSV from a 2D array, this is what I would consider

https://gfx.github.io/perl.js/#use ... (map {'"'.join('","', @$_).'"' } @d), "\n";

But do take note for really large dataset, it will require a lot of memory
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,754
Reaction score
533
At the end of the link, you can see this.
$temp = join "\n", map { $_ = join ",", @{$_} } @data;
This will take the 2d array @data and dump into $temp.
print OUTPUTFILE $temp;

I just couldn't locate the create 2D array through split and join part.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
At the end of the link, you can see this.
$temp = join "\n", map { $_ = join ",", @{$_} } @data;
This will take the 2d array @data and dump into $temp.
print OUTPUTFILE $temp;

I just couldn't locate the create 2D array through split and join part.

That is the same as what I have shown you in my example too. I didn't take reference from the article at all :)

But I won't recommend you doing it for large dataset, the script will need to keep everything in the memory while performing the concatenation.
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,754
Reaction score
533
That is the same as what I have shown you in my example too. I didn't take reference from the article at all :)

But I won't recommend you doing it for large dataset, the script will need to keep everything in the memory while performing the concatenation.
Salute such originality. :D
I will drive until it breaks. It may take a long while since it's 64 bits. :s8:
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top