in reply to Merging of arrays with space

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.


Dave

Replies are listed 'Best First'.
Re^2: Merging of arrays with space
by MynameisAchint (Novice) on Jun 05, 2013 at 06:49 UTC
    Hey

    Mine is a csv file . this is what i have written, the problem is that spaces gets overlooked

    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

      The reason for spaces being skipped is in your line

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

        it worked perfectly fine , exit was just put while debugging . thank you for helping out , really appreciate your help .

      Is your writing of a CSV parser something you are doing for your own educational benefit and/or amusement? Unless you're specifically directed not to get CPAN involved, you should let the proven solution do its job. See Text::CSV.


      Dave

        Hey Actually I am very new to perl I picked it up 4 days back , so I don't have any idea about CPAN , I am writing a utility that converts csv file to text in a specific format and vice versa . So I am not comfortable with CPAN
      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