in reply to Text Conversion

Update: Okay as you explained more, now here is some code works for the sample data you give. For me, the best way to interpret your requirement is, to base everything on the sample you gave, so you may have to make some changes, to make it fit:
use strict; open(IN, "<", "data.txt"); my @in = <IN>; close(IN); open(OUT, ">", "out.txt"); my $out = ""; foreach my $in (@in) { chomp($in); if ($in =~ m/^(.*?)\\(.*?)\s*$/) { $out .= " $2"; } else { if ($out ne "") { print OUT "$out\n"; } $in =~ m/^(.*?)\s*$/; $out = $1; } } print OUT "$out\n"; close(OUT);
Oringinal Reply: The way you store your data is really problematic. Use a flatened structure like _section1, _section2, _section12, _section13 etc is not a good idea at all.

It is much more natural to look at your data structure as a tree. For this purpose, there are lots of implementation choices.

One choice is array-ref of array-ref... So first you would have, $source{_source} which is an array ref. For example, your _section1, would be
$source{_source}->[0]
If your case is that you need to store some text for section1 itself, even though it is not a leaf, then store those things in:
$source{_source}->[0][0]#dedicate 0 to myself
If this section1 is not a leaf, itself would be another array-ref, and its first child would be
$source{_source}->[0][1]#my 1st child
This goes on and on.

There is a big chance that you could make your parse function recursive.

A piece of sample code:
use Data::Dumper; use strict; my %source; $source{_source} = []; $source{_source}->[0] = []; $source{_source}->[0][0] = "section1";#myself $source{_source}->[0][1] = "section11";#1st child $source{_source}->[0][1] = "section12";#2nd child print Dumper(\%source);

Replies are listed 'Best First'.
Re: Re: Text Conversion
by artist (Parson) on Dec 30, 2002 at 20:54 UTC
    Hi,
    To all those, who are attempting to answer, and asking me to change the data structure:
    My source object has methods which can be called from outside. Thus, to the destination object I access the method with source->section1 rather than source->{_section1}. Also section1,section2 are just the place holder here for the actual names such as account_number or key_code.

    Here is the sample data suggested by jdporter.

    foo1 bar1 etc1
    foo21 bar21 etc21 
    foo22 \bar22 etc22
    foo3 bar3 etc3
    foo31 bar31 etc31
    foo32 \bar32 etc32
    foo33 \bar33 etc33
    
    Output required:
    foo1 bar1 etc1
    foo21 bar21 etc21 bar22 etc22
    foo3 bar3 etc3
    foo31 bar31 etc31 bar32 etc32 bar33 etc33
    

    Thanks,
    Artist

Re: Re: Text Conversion
by artist (Parson) on Dec 30, 2002 at 21:52 UTC
    Hi pg,
    I like your reply and it works fine. There is one problem. my input file is about 30 MB and I don't want to store the entire thing in memory as that may create problem.
    So
    my @in = <IN>;
    is not a good option for this case. Also Since I have to parse further, I would like to have 1 items added.At what point I would add that?
    $key = foo_item
    $value = bar_items
    process($key,$value)
    
    Thanks
    Artist