in reply to script to do a foreach on an array of strings outputting the title row and each string element vertically

Your input data doesn't match your script: it's not comma-separated. I added commas as separators and transformed the input data to:
Animal,keepers,M/F,YEAR,DOB,AGE,FSM,PREM ET,ND AL,EAL GIRAFFE,JAMES LE,M,9,10/12/2007,,Y,Y,N,N HIPPOS,JACKIE LEAN,F,6,11/12/2007,,Y,N,Y,Y ZEBRAS,JAMES LEHERN,M,3,12/12/2007,,N,N,N,Y LIONS,AMIE CAHORT,M,1,13/12/2012,,Y,Y,Y,N
and then this code got your expected output:
#!/usr/bin/perl use strict; use warnings; use 5.010; my $file = '1151282.dat'; open my $fh, '<', $file or die $!; my @headers; while ( <$fh> ) { chomp; if ( $. == 1 ) { @headers = split ','; next; } my @strings = split ','; for ( 0 .. @headers - 1 ) { say "$headers[ $_ ] $strings[ $_ ]"; } say ''; } __END__
However, if you really have CSV data, you should use a module to parse it; I recommend Text::CSV_XS

Hope this helps!

The way forward always starts with a minimal test.
  • Comment on Re: script to do a foreach on an array of strings outputting the title row and each string element vertically
  • Select or Download Code