|
MynameisAchint has asked for the wisdom of the Perl Monks concerning the following question:
hey I need to merge two arrays and there can be spaces in the array , so I need to preserve those spaces too i.e. they should be there at their location .
I did this @array_A2=(@array_A2,@array_A1); but the spaces are being overlooked . please help
Re: Merging of arrays with space
by davido (Cardinal) on Jun 05, 2013 at 05:45 UTC
|
This is the point in time where if you still need assistance, you provide us with a self-contained chunk of between 5 and 15 lines of code that demonstrates your problem. The code you provided already isn't complete enough to illustrate what you're having trouble with.
If the code you provided doesn't produce the results you expect, you probably should inspect what the two arrays actually contain prior to being appended together.
In your earlier posts it looked like you were either parsing CSV files or pulling data from an Excel spreadsheet. My guess is that part of your script is broken, and that the "space" that's getting skipped is due to bad parsing.
| [reply] |
|
|
sub column_segregation{
$column_number=$_[0];
my $size;
my @array_A1=();
my @array_A2=();
my $column_separator = ",";
$column_number--;
my $file="Working_On.csv";
open(FILE,"<","$file");
my @lines=<FILE>;
close FILE;
foreach my $line (@lines)
{
my @columns = split(/$column_separator/,"$line");
@array_A1= split (/$column_separator/,"$columns[$column_number]");
+
@array_A2=(@array_A2,@array_A1);
+
exit}
return (@array_A2);
}
so if you can point out why the spaces are being overlooked , the above functions separates out the column number from csv file into array
| [reply] [d/l] |
|
|
@array_A1= split (/$column_separator/,"$columns[$column_number]");
If $columns[$column_number] is the empty string (not " " but ""), then your split will return an empty array, ie you lose your blank cell you are looking for.
I would think instead of
@array_A1= split (/$column_separator/,"$columns[$column_number]");
@array_A2=@array_A2,@array_A1);
you should just do
push @array_A2, $columns[$column_number];
I assume the exit statement is left from debugging but it will stop execution of the script immediately which makes no sense at this point.
| [reply] [d/l] [select] |
|
|
|
|
| [reply] |
|
|
|
|
|
|
I don't understand why you are doing split twice (first for the row and then for the column).
If you just want to return a given column from a CSV you could do it simply with:
my @result = map {
my @row = split /,/ => $_;
$row[$column_number] } @lines;
assuming there isn't any escaped csv-values such as
a,b,"hello, world",c,d
| [reply] [d/l] [select] |
Re: Merging of arrays with space
by hdb (Monsignor) on Jun 05, 2013 at 05:32 UTC
|
use strict;
use warnings;
my @array_A1 = ( " ", "1" );
my @array_A2 = ( "2", " ", "3" );
@array_A2=(@array_A2,@array_A1);
print join ",", @array_A2;
| [reply] [d/l] |
Re: Merging of arrays with space
by vinoth.ree (Monsignor) on Jun 05, 2013 at 05:37 UTC
|
use strict;
use warnings;
use Data::Dumper;
my @arr1 = (1,23,'',45," ",4);
my @arr2 = ("vinoth", " kumar", " ", 'vin' );
@arr1 = (@arr1,@arr2);
print Dumper \@arr1
Output:
$VAR1 =
1,
23,
'',
45,
' ',
4,
'vinoth',
' kumar',
' ',
'vin'
;
All is well | [reply] [d/l] |
|
|