Hello

Thanks for your help

name1&firstname1&adresse1&firstname4&adresse4&firstname6&adresse6
name2&firstname2&adresse2&firstname5&adresse5&firstname7&adresse7
name3&firstname3&adresse3&name6
name4&name7
name5


What this means 

if( ! length() )




________________________________
From: Shawn H Corey <shawnhco...@gmail.com>
To: Jackie Jackie <j_jacki...@yahoo.ca>
Cc: beginners@perl.org
Sent: Sun, November 15, 2009 3:51:04 PM
Subject: Re: join lines

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.



      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/

Reply via email to