Help - Search - Member List - Calendar
Full Version: how to append blocks in same file
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
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

Errin M HMMA/IT Larsen
Aditi Gupta wrote:
QUOTE
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

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:
QUOTE
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

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 Removed]> wrote:
QUOTE

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 Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




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 Removed]> wrote:
QUOTE

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 Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




Dave Gray
On 6/22/05, Aditi Gupta <[Email Removed]> wrote:
QUOTE
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 (<DATA>) {
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

Dan Klose
QUOTE
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

QUOTE

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 (<DATA>) {
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

Dave Gray
On 6/22/05, Dan Klose <[Email Removed]> wrote:
QUOTE

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?

Wijaya Edward
QUOTE
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.b...638b6eb94a626d2

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

John W. Krahn
Aditi Gupta wrote:
QUOTE
Hi Perlers,

Hello,

QUOTE
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

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 (<DATA>) {
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 (<FH>)
{
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 Removed]> wrote:
QUOTE

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 Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




John W. Krahn
Aditi Gupta wrote:
QUOTE

On 6/23/05, John W. Krahn <[Email Removed]> 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 "$linen";
}

__END__



John
--
use Perl;
program
fulfillment


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.