Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: perl: How to comapre and manipulate csv file in perl?

by misterperl (Pilgrim)
on Oct 01, 2014 at 14:53 UTC ( [id://1102545]=note: print w/replies, xml ) Need Help??


in reply to perl: How to comapre and manipulate csv file in perl?

I have many comments on your code , but Im not really sure what you're trying to accomplish so I'l keep it short.

It seems like you're trying to operate on 2-row blocks? Like if the first row meets some match criteria, then meld it into the next row?

...then maybe break up the file into 2-row blocks and use regexes on each one.. Like split it this way:

my @tworows = split /[^\n]+\n[^[n]+\n/, $text;

then you can just use something like

s/((\w+).+\1.+\1.+)\n(.+)/something/

then repeat that starting on the first even row (because the regex always checks the FIRST row for matches). Much simpler than storing all those temporary arrays, whiles, ifs, etc.

But here are few recomendations:

1. next; } is the same as } 2. Don't make a copy of your row array to use it, just dereference it: my @row1 = @{$row}; # not needed.. Just use $row->[$index] instead of $row1[$index] 3. drop all the extra parens join ';', @arr; push @arr, 'something'; 4. it looks like you're using a numeric == to compare ascii fields lik +e $row1[0] == $row2[0] when col 0 contains things like 'PF'. Use eq f +or these compares. You can't use == there.

Log In?
Username:
Password:

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

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

    No recent polls found