Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Better Way to Do it 2

by cored (Scribe)
on Aug 27, 2003 at 22:01 UTC ( [id://287183]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, i wrote this program before when i saw the solution of one of the exercicies on the perlmonks page that are very good actually i found the code again and try to make it smaller if you want you can help me with this too :D
#!perl -w #This is the first version use strict; my $file; print "Enter a filename: "; chomp ( $file = <STDIN> ); open ( FILE, "$file" ); for (reverse (<FILE>)){ print; } close FILE; # This is the second version :D very small perl -e "print for (reverse(<>))";
Do you have suggest just make your replys :d

Replies are listed 'Best First'.
Re: Better Way to Do it 2
by BrowserUk (Patriarch) on Aug 27, 2003 at 22:39 UTC

    Not shorter, but different:)

    perl -e"sub x{local$_=<>;eof||&x;print};&x" < file

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Better Way to Do it 2
by tedrek (Pilgrim) on Aug 27, 2003 at 23:02 UTC

    and still shrinking

    perl -pi -e '$_=reverse'

    Update: yep as MarkM points out this reverses each line, not the order of lines :(.

      Unless I'm missing something, your solution doesn't work. You are using reverse in scalar context, which reverses the order of the string, not the order of the lines.

Re: Better Way to Do it 2
by NetWallah (Canon) on Aug 27, 2003 at 22:17 UTC
    Very similar, same function, but without explicit loop.
    The "-n" supplies the loop necessary:
    perl -ne "print scalar reverse"
    Update: Oops - as folks have pointed-out(++) this reverses characters, not lines - I misread the original post.
    perl -ne "print reverse <>" < file-name
    Does the write thing!
      Ahh-whoops... not the same output as above.

      The <> above is grabbing the whole file and reading all the lines into a list.   Then reverse is reversing the order of the elements, thus reversing the order of the lines.   Then the print for( ) is printing those lines one at a time.   So the file is output in reverse, last line first and first line last, but not reversing the characters within the lines.

      tedrek's code duplicates your functionality, reversing the characters of each line as it is read starting from the first line.

      Now BrowserUk's code does the same thing as desired above.   Except that it wants to crash Perl on any file too lengthy ....     (beware the smiling monk who knows the fate awaiting you)

      You can even leave out the "for"!

      (The <> reads the file into a list and the reverse flips the list, which is then passed to print):

      perl -e 'print reverse <>' filename
Re: Better Way to Do it 2
by xChauncey (Scribe) on Aug 27, 2003 at 22:31 UTC
    Hmmm, probably the first one-liner I've seen that I actually understand.
    Clobber me if I'm wrong, and I realize that this is not the thrust of your post, but isn't passing a user-given string to open an invite for the user to do unexpected things to your files? (e.g. user types ">filename" and clobbers a file)
      but isn't passing a user-given string to open an invite for the user to do unexpected things to your files?

      Uhm, no. The user might clobber a file he already has write access to (probably his own), but that's it; there's no increased risk, his shell allows him to mess up his files already. Unless you run that program suid, but there's no need for that.

      Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found