how to append blocks in same file

2005-06-22 Thread Aditi Gupta
Hi Perlers,

I have a file as follows:

#block1
aaa
aaa

#block2
bbb
bbb

#block3
ccc
ccc

and i want to append(or i should say merge) these blocks and get a file like 
this:

aaabbbccc
aaabbbccc

how can this be done using perl?
please help..

regards
Aditi


RE: how to append blocks in same file

2005-06-22 Thread Larsen, Errin M HMMA/IT
Aditi Gupta wrote:
> Hi Perlers,
> 
> I have a file as follows:
> 
> #block1
> aaa
> aaa
> 
> #block2
> bbb
> bbb
> 
> #block3
> ccc
> ccc
> 
> and i want to append(or i should say merge) these blocks and get a
> file like this:
> 
> aaabbbccc
> aaabbbccc
> 
> how can this be done using perl?
> please help..
> 
> regards
> Aditi

Hi,

That looks like a fun and challenging problem.  I bet Perl would be good
at that.

Have you tried any Perl code yet?

This list works best when you present us some code you have tried that
is not working.  Then, we can help you fix it.  There are many on the
list who would be happy to write code for you for a reasonable fee,
however.  Is that what you are looking for?

--Errin

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: how to append blocks in same file

2005-06-22 Thread Dan Klose
If I was doing this I would split the file on the gaps between blocks
and the use the unix paste function to assemble the blocks.

&>  paste A B C > D

? using a hash of arrays (HOA) to store each block and then go back over
the HOA each element at a time ... although this is probably overkill?

On Wed, 2005-06-22 at 09:10 -0500, Larsen, Errin M HMMA/IT wrote:
> Aditi Gupta wrote:
> > Hi Perlers,
> > 
> > I have a file as follows:
> > 
> > #block1
> > aaa
> > aaa
> > 
> > #block2
> > bbb
> > bbb
> > 
> > #block3
> > ccc
> > ccc
> > 
> > and i want to append(or i should say merge) these blocks and get a
> > file like this:
> > 
> > aaabbbccc
> > aaabbbccc
> > 
> > how can this be done using perl?
> > please help..
> > 
> > regards
> > Aditi
> 
> Hi,
> 
> That looks like a fun and challenging problem.  I bet Perl would be good
> at that.
> 
> Have you tried any Perl code yet?
> 
> This list works best when you present us some code you have tried that
> is not working.  Then, we can help you fix it.  There are many on the
> list who would be happy to write code for you for a reasonable fee,
> however.  Is that what you are looking for?
> 
> --Errin
> 
-- 
Daniel Klose
Mathematical Biology
NIMR
The Ridgeway
Mill Hill
London
NW7 1AA


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-22 Thread Aditi Gupta
Hi all,

I know the terms of the list.. and before this i have always written a code 
and asked for a doubt if it doesn't work... but this time i'm not aware of 
any way to do the job.. thats why i asked to help me out, I haven't asked 
for any code, just hints what can be used. I'm sorry if i gave the wrong 
impression, i was expecting guidelines, not code.

regards,
aditi

On 6/22/05, Larsen, Errin M HMMA/IT <[EMAIL PROTECTED]> wrote:
> 
> Aditi Gupta wrote:
> > Hi Perlers,
> >
> > I have a file as follows:
> >
> > #block1
> > aaa
> > aaa
> >
> > #block2
> > bbb
> > bbb
> >
> > #block3
> > ccc
> > ccc
> >
> > and i want to append(or i should say merge) these blocks and get a
> > file like this:
> >
> > aaabbbccc
> > aaabbbccc
> >
> > how can this be done using perl?
> > please help..
> >
> > regards
> > Aditi
> 
> Hi,
> 
> That looks like a fun and challenging problem. I bet Perl would be good
> at that.
> 
> Have you tried any Perl code yet?
> 
> This list works best when you present us some code you have tried that
> is not working. Then, we can help you fix it. There are many on the
> list who would be happy to write code for you for a reasonable fee,
> however. Is that what you are looking for?
> 
> --Errin
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 
>


Re: how to append blocks in same file

2005-06-22 Thread Aditi Gupta
HI Dan,

Since i'm using activestate perl on windows xp i don't know whether paste 
will work or not. 
I'll try the hash of array. 

Thanks for the help

regards
Aditi.

On 6/22/05, Dan Klose <[EMAIL PROTECTED]> wrote:
> 
> If I was doing this I would split the file on the gaps between blocks
> and the use the unix paste function to assemble the blocks.
> 
> &> paste A B C > D
> 
> ? using a hash of arrays (HOA) to store each block and then go back over
> the HOA each element at a time ... although this is probably overkill?
> 
> On Wed, 2005-06-22 at 09:10 -0500, Larsen, Errin M HMMA/IT wrote:
> > Aditi Gupta wrote:
> > > Hi Perlers,
> > >
> > > I have a file as follows:
> > >
> > > #block1
> > > aaa
> > > aaa
> > >
> > > #block2
> > > bbb
> > > bbb
> > >
> > > #block3
> > > ccc
> > > ccc
> > >
> > > and i want to append(or i should say merge) these blocks and get a
> > > file like this:
> > >
> > > aaabbbccc
> > > aaabbbccc
> > >
> > > how can this be done using perl?
> > > please help..
> > >
> > > regards
> > > Aditi
> >
> > Hi,
> >
> > That looks like a fun and challenging problem. I bet Perl would be good
> > at that.
> >
> > Have you tried any Perl code yet?
> >
> > This list works best when you present us some code you have tried that
> > is not working. Then, we can help you fix it. There are many on the
> > list who would be happy to write code for you for a reasonable fee,
> > however. Is that what you are looking for?
> >
> > --Errin
> >
> --
> Daniel Klose
> Mathematical Biology
> NIMR
> The Ridgeway
> Mill Hill
> London
> NW7 1AA
> 
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 
>


Re: how to append blocks in same file

2005-06-22 Thread Dave Gray
On 6/22/05, Aditi Gupta <[EMAIL PROTECTED]> wrote:
> Since i'm using activestate perl on windows xp i don't know whether paste
> will work or not.
> I'll try the hash of array.

Using a hash of arrays will not necessarily preserve the order. Below
is the start of an array of arrays solution. I'll leave handling
unbalanced blocks and reassembling the pieces as an exercise for the
reader.

my @blocks = ();
my $maxlen = 0;
while () {
chomp;
if (/^#/) {
push @blocks, [];
} elsif ($_) {
push @{$blocks[$#blocks]}, $_;
my $len = $#{$blocks[$#blocks]};
$maxlen = $len if $len > $maxlen;
}
}

__DATA__
#block1
aaa
ddd

#block2
bbb
eee

#block3
ccc
fff

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-22 Thread Dan Klose

> Using a hash of arrays will not necessarily preserve the order...

If you don't want to worry about sorting the order of the blocks i.e.
doing it yourself then use Tie::IXhash (something like that on CPAN) to
preserve the order, you then don't have to worry about unbalanced blocks
and reassembly by applying an if defined type block. Easy.

Dan

> 
> Using a hash of arrays will not necessarily preserve the order. Below
> is the start of an array of arrays solution. I'll leave handling
> unbalanced blocks and reassembling the pieces as an exercise for the
> reader.
> 
> my @blocks = ();
> my $maxlen = 0;
> while () {
> chomp;
> if (/^#/) {
> push @blocks, [];
> } elsif ($_) {
> push @{$blocks[$#blocks]}, $_;
> my $len = $#{$blocks[$#blocks]};
> $maxlen = $len if $len > $maxlen;
> }
> }
> 
> __DATA__
> #block1
> aaa
> ddd
> 
> #block2
> bbb
> eee
> 
> #block3
> ccc
> fff
> 
-- 
Daniel Klose
Mathematical Biology
NIMR
The Ridgeway
Mill Hill
London
NW7 1AA


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-22 Thread Dave Gray
On 6/22/05, Dan Klose <[EMAIL PROTECTED]> wrote:
> 
> > Using a hash of arrays will not necessarily preserve the order...
> 
> If you don't want to worry about sorting the order of the blocks i.e.
> doing it yourself then use Tie::IXhash (something like that on CPAN) to
> preserve the order, you then don't have to worry about unbalanced blocks
> and reassembly by applying an if defined type block. Easy.

Almost as easy as giving someone a complete working example to their
homework assignment without them having to learn anything?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-22 Thread Wijaya Edward
> and i want to append(or i should say merge) these blocks and get a 
> file like  this:

> aaabbbccc
> aaabbbccc


I've asked the same question some time ago,
check this out:
http://groups-beta.google.com/group/perl.beginners/browse_thread/thread/4f28012f744d10e0/7638b6eb94a626d2?q=edward+wijaya+group:perl.beginners&rnum=146&hl=en#7638b6eb94a626d2

But please make attempt to write your own script first,
no matter how lousy it is. 
Show respect to the time given by people here.

--
Regards,
Edward WIJAYA
Singapore


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-22 Thread John W. Krahn

Aditi Gupta wrote:

Hi Perlers,


Hello,


I have a file as follows:

#block1
aaa
aaa

#block2
bbb
bbb

#block3
ccc
ccc

and i want to append(or i should say merge) these blocks and get a file like 
this:


aaabbbccc
aaabbbccc

how can this be done using perl?
please help..



$ echo "
aaa
aaa

bbb
bbb

ccc
ccc
" | perl -l -00ne'my $i; $x[ $i++ ] .= $_ for split /\n/ }{ print for @x'
aaabbbccc
aaabbbccc



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to append blocks in same file

2005-06-24 Thread Aditi Gupta
hello everybody,

first of all i'd like to thank all of you for your time and help.. but i'm 
still stuck!!

The code which Dave sent:

my @blocks = ();
my $maxlen = 0;
while () {
chomp;
if (/^#/) {
push @blocks, [];
} elsif ($_) {
push @{$blocks[$#blocks]}, $_;
my $len = $#{$blocks[$#blocks]};
$maxlen = $len if $len > $maxlen;
}
}

I couldn't understand the line my $len = $#{$blocks[$#blocks]};
i referred to the programming perl book, i didn't understood why $# was used 
twice..

and in the John's mail.. 

$ echo "
aaa
aaa

bbb
bbb

ccc
ccc
" | perl -l -00ne'my $i; $x[ $i++ ] .= $_ for split /\n/ }{ print for @x'

I didn't understood the " | perl -l -00 " part.. please explain what is 
means..

Since i'm not good in using comples data structures, i obtained the 
different blocks in differernt text files using the following code:

---
#!perl/bin/perl -w

use strict;

my $n = 7;
my $count=0;
my @aoa;
my $file = "out.txt";
open (FH, $file) or die "couldn't open $file:$!";
while ()
{
push @aoa, [split/\n/];
}
print "\n No.of rows: $#aoa";

my $last_val= ($#aoa/$n);
my $lv = sprintf ("%.0f", $last_val);
print "last val: $lv";

for(my $f=1;$f<$lv;$f++)
{
$file = "$f.txt";
open (OF, ">$file") or die "couldn't open $file:$!";
for my $i ($count..($n+$count))
{
my $j=0;
#{
print OF "$aoa[$i][$j]\n";
$count++;
#}
}

}


--
But I want to do it using the complex data structures. Hence, please explain 
the points which I couldn't understand. 

Thanks again,
with regards,

Aditi


On 6/23/05, John W. Krahn <[EMAIL PROTECTED]> wrote:
> 
> Aditi Gupta wrote:
> > Hi Perlers,
> 
> Hello,
> 
> > I have a file as follows:
> >
> > #block1
> > aaa
> > aaa
> >
> > #block2
> > bbb
> > bbb
> >
> > #block3
> > ccc
> > ccc
> >
> > and i want to append(or i should say merge) these blocks and get a file 
> like
> > this:
> >
> > aaabbbccc
> > aaabbbccc
> >
> > how can this be done using perl?
> > please help..
> 
> 
> $ echo "
> aaa
> aaa
> 
> bbb
> bbb
> 
> ccc
> ccc
> " | perl -l -00ne'my $i; $x[ $i++ ] .= $_ for split /\n/ }{ print for @x'
> aaabbbccc
> aaabbbccc
> 
> 
> 
> John
> --
> use Perl;
> program
> fulfillment
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 
>


Re: how to append blocks in same file

2005-06-24 Thread John W. Krahn

Aditi Gupta wrote:


On 6/23/05, John W. Krahn <[EMAIL PROTECTED]> wrote:


Aditi Gupta wrote:


I have a file as follows:

#block1
aaa
aaa

#block2
bbb
bbb

#block3
ccc
ccc

and i want to append(or i should say merge) these blocks and get a file 
like this:


aaabbbccc
aaabbbccc

how can this be done using perl?
please help..



$ echo "
aaa
aaa

bbb
bbb

ccc
ccc
" | perl -l -00ne'my $i; $x[ $i++ ] .= $_ for split /\n/ }{ print for @x'
aaabbbccc
aaabbbccc


I didn't understood the " | perl -l -00 " part.. please explain what is 
means..


That is short for:

#!/usr/bin/perl -w
use strict;

$/ = '';  # set paragraph mode - switch -00

my @data;
while ( <> ) {
my $i;
for my $line ( split /\n/ ) {
$data[ $i++ ] .= $line;
}
}

for my $line ( @data ) {
print "$line\n";
}

__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]