Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Writing in reverse

by Anonymous Monk
on Aug 01, 2000 at 04:01 UTC ( #25368=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do you write the characters in certain array of the first file in reverse order merging with the array on a second file in a reverse order too. For example, File 1 contains: abcde; and File2 contains: ijklm .I want to write: me ld kc jb ia. Thankx perl masters!

Replies are listed 'Best First'.
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.

      @first = reverse split //, <FILE1>;
      will only operate on the first line of the file because split forces the filehandle into a scalar context.
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 "; }
      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.
        never mind..I got it
      You fell into the same trap as chromatic in that you only read the first line of the file. Either localize $/ to undef or use my @file1 = <$fh1>

      Other then that though, this is very slick and you use a much better way of scoping the file handle.

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);

      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/>
        I see your point. Thank you :)
      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);
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://25368]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-05-18 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?