new to Perl - CSV - find a string and print all numbers in that column -



new to Perl - CSV - find a string and print all numbers in that column -

i've got bunch of info in csv file, first row strings (all text , underscores), subsequent rows filled numbers relating said strings.

i'm trying parse through first line , find particular strings, remember column string in, , go through rest of file , info in same column. need 3 strings.

i've been using text::csv can't figure out how increment counter until finds string in first line , go next line, info same column, etc. etc. here's i've tried far:

while (<csv>) { if ($csv->parse($data)) { @field = $csv->fields; $count = 0; $column (@field) { print ++$count, " => ", $column, "\n"; } } else { $err = $csv->error_input; print "failed parse line: $err"; } }

since $data in line 1, prints "1 $data" 25 times (# of lines in csv file). how remember column found $data in? also, since know of strings in line 1, how parse through line 1, find of strings in @data, , parse through rest of file, grabbing info necessary columns , putting matrix or array of arrays? help!

edit: realized questions bit poorly phrased. don't know how column number csv. how done? also, 1 time i've got column number, how tell csv run through subsequent lines , grab info column?

try this:

use strict; utilize warnings; utilize text::csv; $csv = text::csv->new({binary=>1}); $thing_to_match = "blah"; $matched_index; @stored_data = (); while(my $row= $csv->getline(*data)) #grabs lines below __data__ #(near end of script) { @fields = @$row; #if haven't found matched index, yet, search it. if(not defined $matched_index) { foreach $i(0..$#fields) { $matched_index = $i if($fields[$i] eq $thing_to_match); } } #note: we're pushing *reference* array! #look @ perldoc perldata force @stored_data,\@fields; } die "column '$thing_to_match' not found!" unless defined $matched_index; foreach $row(@stored_data) { print $row->[$matched_index] . "\n"; } __data__ stuff,more stuff,yet more stuff "yes, thing, 1 item",blah,blarg 1,2,3

the output is:

more stuff blah 2

perl csv

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -