Jackie Jackie wrote: > I tried to adapt this code to obtain my desired output. I need help. My logic > is > > While > if lines are not empty {do} > if lines are empty {do} > > While > if lines are not empty {do} > else for empty lines{do} > > I am new to programming. I have problem with logic and syntax. So please > explain to me how I solve this problem.
Your data that is divided into sets separated by blank lines and these sets divided into lists separated by lines with only a single ampersand. The flaw is that you reset the index to the beginning of the data when you should reset it to the beginning of the set. > use strict; > use warnings; > my @ar = []; my $begin_set = 0; > my $i = 0; > > while (<DATA>) { > chomp; next if !length(); chomp; if( ! length() ){ $begin_set = @ar; $i = $begin_set; } > if (/^\s*&\s*$/) { > $i = 0; next; $i = $begin_set; next; > } > push @{$ar[$i]}, @{$ar[$i++]} ? '&'.$_ : $_; > > } > > for my $ar (@ar) { > print @{$ar},"\n"; } > > > __DATA__name1 __DATA__ name1 > name2 > name3 > & > firstname1 > firstname2 > firstname3 > & > adresse1 > adresse2 > adresse3 > > name4 > name5 > & > firstname4 > firstname5 > & > adresse4 > adresse5 > > name6 > name7 > & > firstname6 > firstname7 > & > adresse6 > adresse7 -- Just my 0.00000002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/