in reply to Re^2: Determine if array value in string results
in thread Determine if array value in string results
Hi again. First, no way you should be producing your output like that. That kind of manual jiggery-pokery is fraught with pitfalls and error vectors. You need to build up a data structure known as an array of arrays representing your output, and then convert it to the delimited file using Text::CSV or Text::CSV_XS.
As to your problem: you are building a lookup table from the wrong structure. If what you want to do is check all fields in @csvfieldlist to see which are present in @fields, and record a null value for those that are not, you need to make the lookup table from @fields and loop through the larger list.
use Text::CSV_XS; my @rows; for my $line ( @something ) { if ( $line =~ /NRG\ location/) { my @row; my @fields; foreach my $bline (@blocklines) { my ($k, $v) = split '=', $bline; push @fields, $k; } my %lookup = map { $_ => 1 } @fields; for my $field ( @csvfieldlist ) { if ( exists $lookup{ $field } ) { push @row, 'yes'; } else { push @row, undef; } } push @rows, \@row; } } csv (in => \@rows, out => "file.csv", sep_char=> ";");
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Determine if array value in string results
by ImJustAFriend (Scribe) on Nov 26, 2018 at 16:26 UTC |