Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Merging of arrays with space

by MynameisAchint (Novice)
on Jun 05, 2013 at 06:49 UTC ( [id://1037139]=note: print w/replies, xml ) Need Help??


in reply to Re: Merging of arrays with space
in thread Merging of arrays with space

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

Replies are listed 'Best First'.
Re^3: Merging of arrays with space
by hdb (Monsignor) on Jun 05, 2013 at 07:11 UTC

    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 .

Re^3: Merging of arrays with space
by davido (Cardinal) on Jun 05, 2013 at 08:15 UTC

    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

        At the command prompt type: cpan install Text::CSV, then read Text::CSV. Presto, a CSV parser that works well, and that is well understood and supported by the Perl community. :)

        And congratulations; you've just installed your first CPAN module. There are some who will tell you that CPAN is the language, and Perl is the syntax that glues it together. That's probably overstating the cause, but I do think it's accurate to say that Perl wouldn't be modern Perl without CPAN.


        Dave

Re^3: Merging of arrays with space
by jnyman (Acolyte) on Jun 05, 2013 at 07:31 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1037139]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-23 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found