Re: Writing in reverse
by chromatic (Archbishop) on Aug 01, 2000 at 04:22 UTC
|
Untested code follows. It's highly inefficient, but you get the idea:
my (@first, @second);
{
local *FILE1;
local *FILE2;
open(FILE1, $file1) || die "Can't open $file1: $!";
open(FILE2, $file2) || die "Can't open $file2: $!";
@first = reverse split(//, <FILE1>);
@second = reverse split(//, <FILE2>);
close FILE1;
close FILE2;
}
while (@first) {
my $couplet = shift @first . shift @second;
print "$couplet ";
}
A more efficient technique would involve substr and not that yucky reverse split magic, which may not work anyway. (I'm not at a machine where I can test this right now.)
Update: chop is a better solution yet. | [reply] [d/l] |
|
|
@first = reverse split //, <FILE1>;
will only operate on the first line of the file because
split forces the filehandle into a scalar context.
| [reply] [d/l] |
Re: Writing in reverse
by eak (Monk) on Aug 01, 2000 at 06:42 UTC
|
Here is another way to do it using chop.
#!/usr/bin/perl -w
use IO::File;
my $fh1 = new IO::File;
my $fh2 = new IO::File;
$fh1->open("< file1.txt") or die "can't open file1.txt $!";
$fh2->open("< file2.txt") or die "can't open file2.txt $!";
my $file1 = <$fh1>;
my $file2 = <$fh2>;
print "Merging:\n";
print "\t$file1\n";
print "\t$file2\n";
while(length $file1){
my $couplet = chop($file1).chop($file2);
print "$couplet ";
}
| [reply] [d/l] |
|
|
Eak,
Thankx for the answer, What if I want the arrays to merge in backward order? like first file is : abcde, second file is fghij, I want ej di ch bg af. By the way it only worked for one array only rather than whole file . It didn't read whole file for some reason. Thankx.
| [reply] |
|
|
| [reply] |
|
|
|
|
| [reply] |
RE: Writing in reverse
by larsen (Parson) on Aug 01, 2000 at 14:01 UTC
|
What about this solution?
# Two string of the SAME length?
my $length;
open( FILE1, "string1" );
open( FILE2, "string2" );
$string1 = <FILE1>;
$string2 = <FILE2>;
map {
$a = substr($string1, $_, 1);
$b = substr($string2, $_, 1);
print "$b$a ";
} reverse(0 .. length($string1) - 1); | [reply] |
|
|
If you edit your node and put <CODE> ... </CODE> tags around the code then it will be far more legible and your <FILE1> and <FILE> expressions won't be interpeted as HTML tags.
At the same time, you should probably consider checking the return value from the open function. I realise that this is just an example, but people will copy and paste it, so you should make it as safe as possible.
--
<http://www.dave.org.uk>
European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/>
| [reply] |
|
|
I see your point. Thank you :)
| [reply] |
|
|
Excuse me for my badly formatted reply.
I've missed to RTF.How To display...:>
Here the piece of code I suggest:
my $length;
open( FILE1, "string1" );
open( FILE2, "string2" );
$string1 = <FILE1>;
$string2 = <FILE2>;
map {
$a = substr($string1, $_, 1);
$b = substr($string2, $_, 1);
print "$b$a ";
} reverse(0 .. length($string1) - 1);
| [reply] |