Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Read reverse from a file

by stamp1982 (Novice)
on Jun 30, 2013 at 22:54 UTC ( #1041667=perlquestion: print w/replies, xml ) Need Help??
stamp1982 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I need help with my perl code to read from a .txt file and print out in reverse order. Write a Perl program to read a file, and then print its lines in reverse order, the last line first. This is what I have so far but I am stuck and it would not work. What am I missing out

# Open a .txt file open(seq1, "<seq1.txt") or die $!; #Assign open files to arrays my @seq1 = <seq1>; print "@seq1"; print reverse @seq1;

Replies are listed 'Best First'.
Re: Read reverse from a file
by davido (Archbishop) on Jun 30, 2013 at 23:25 UTC

    We don't know in what way it's not working. ...well, I can see one way. But I need to hear from you what messages you're getting (if any), or what "would not work" means. Example input and the output it produces, along with the output you desire, are all important information so that we don't have to guess at your needs, or at what's wrong.

    Please follow up in this thread with example input, expected output, and exactly what you're seeing instead. Then we can explain why you are seeing what you are seeing, and what needs to change so that you can see what you want.


Re: Read reverse from a file
by smls (Friar) on Jun 30, 2013 at 23:30 UTC

    The line

    print "@seq1";

    was probably intended to do something different than what it actually does.
    Other than that, the program should work. If you remove that line, then afaict the program's function will be exactly as you specified: "read a file, and then print its lines in reverse order".

      Thanks! it did work!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041667]
Approved by ww
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)
[ambrus]: Ok, the docs is somewhat unclear. It does say that when an object is garbage collected, it will get cleaned up, and eventually can no longer get messages. It's not clear how long this takes, eg. I think it's kept alive until its queued events are handled
[ambrus]: in the loop, and I'm not sure if that's ok for AnyEvent. Also, it's not clear if a Timer or File object you free really is garbage collected, i.e. that Prima doesn't keep some references to it, but I hope so.
[Corion]: choroba: No, I don't remember that story, but yes, it matches my experience ;))
[ambrus]: Hopefull the object isn't kept alive, the events are processed immediately, but you'd have to read a lot of source code to be sure about that.
[Corion]: ambrus: I think both of AnyEvent and Prima are pretty tight in their memory management because they both are cooperative multitasking and (I think) both use the Perl memory management for managing things
[Corion]: ambrus: And for Windows, I don't think that Prima knows if there still are messages queued for an object (in the Windows message loop). Finding that out would take lots of effort for little gain
[ambrus]: And even if this works, I'm still not sure you can't get double timeouts from a Timer.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2016-12-09 10:23 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.