Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Writing files in reverse..and more

by Anonymous Monk
on Jul 28, 2000 at 18:52 UTC ( #24859=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks, Good day, Suppose in some file, One array contains "fred and deena // married ". Now I want to open up the file and write this: "aneed dna dref"( It is just a backward order of fred and deena) and I need to get rid of "//married"part. Any suggestions? Thank you .

Comment on Writing files in reverse..and more
Perhaps, perhaps, perhaps?
by gryng (Hermit) on Jul 28, 2000 at 18:56 UTC
    while (<>) { s/\W*\/+.*//; print reverse $_ }
    Not tested, but should work, no? (Sorry for the .* Ovid :) )

    Ciao,
    Gryn

    Update: Escaped my slashes, opps. :) (Thanks fastolfe).

      I would do the regexp like this: s~\s*//.*~~; Using a delimiter other than / keeps you from having to escape the slashes, otherwise it'd be like s/\s*\/\/.*//, which is kinda confusing. But yah the rest is just that simple. Except you aren't considering newlines:

      while (<>) { chomp; s~\s*//.*~~; print reverse $_; print "\n"; }
      gryng: Actually, I recall mentioning that it's not always possible to avoid the dot star. I should have mentioned that sometimes it's a good choice. You found one of those "sometimes" :)

      Cheers,
      Ovid

Re: Writing files in reverse..and more
by davorg (Chancellor) on Jul 28, 2000 at 18:56 UTC

    Something like this perhaps (untested)

    open(IN, 'in.txt') or die "Can't open input file: $!\n"; open(OUT, '>out.txt') or die "Can't open output file: $!\n"; while (<IN>) { my ($stuff, $dregs) = split m[ // ]; print OUT reverse($stuff), "\n"; }
    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Writing files in reverse..and more
by lhoward (Vicar) on Jul 28, 2000 at 18:59 UTC
    my $c="fred and deena // married"; $c=~s/\/\/.*$//; print "".(reverse split //,$c)."\n";
RE: Writing files in reverse..and more
by mikfire (Deacon) on Jul 28, 2000 at 20:19 UTC
    You kids are verbose. A round of Perl Golf, shall we?
    print scalar( reverse substr( $_, 0, rindex($_," //") ) ) while (<>);
    Update davorg is correct - I fixed a few syntax errors. I had the "\n" on the command line I used to test, but apparently missed it when submitting the article. davorg's solution is better than mine - the map is a nice touch.

    mikfire

      Cheat! You just corrected the first problem that I saw (incorrect arguments to rindex).

      The other problem is that it runs all of the lines together, you need to print "\n" at the end of each line like this:

      print scalar( reverse substr( $_, 0, rindex($_," //") ) ), "\n" while +(<>);

      but if we're playing golf then you can save a couple of strokes by using map

      print map { scalar reverse(substr( $_, 0, rindex($_," //") )), "\n" } +<>;
      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>
        A slightly more full featured try with a few more strokes (outside the map), using davorgs origional post meathod of spliting.
        { local $\="\n" => select STDOUT; # Or what ever FileHandle map { print scalar reverse( (split m!\s+//!)[0] ) } <DATA> }
        Enjoy!
        --
        Casey
        
      salut monkey tu es con et encule
You da Monks!
by Anonymous Monk on Jul 28, 2000 at 20:39 UTC
    Damn, you guys are good in perl.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2015-06-30 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What kind of chocolate gives you the most pleasure?















    Results (797 votes), past polls