Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Merging of arrays with space

by MynameisAchint (Novice)
on Jun 05, 2013 at 05:27 UTC ( #1037127=perlquestion: print w/replies, xml ) Need Help??
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

Replies are listed 'Best First'.
Re: Merging of arrays with space
by davido (Archbishop) 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.



      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.

        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.


        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
Re: Merging of arrays with space
by hdb (Prior) on Jun 05, 2013 at 05:32 UTC

    Your code works.

    use strict; use warnings; my @array_A1 = ( " ", "1" ); my @array_A2 = ( "2", " ", "3" ); @array_A2=(@array_A2,@array_A1); print join ",", @array_A2;
Re: Merging of arrays with space
by vinoth.ree (Monsignor) on Jun 05, 2013 at 05:37 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037127]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-06-29 14:57 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (671 votes). Check out past polls.