By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,841 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,841 IT Pros & Developers. It's quick & easy.

HELP! Using a stack to print a file backwards?

P: 74
Hey everybody.

I'm having some difficulty. I know how to make a stack in Perl, but I have no idea how to get it to read from a file then print the contents backwards.

Can anybody give me any ideas on how I might do this?

Thanks in advance!
Feb 7 '08 #1
Share this Question
Share on Google+
9 Replies


eWish
Expert 100+
P: 971
I am not sure if this is what you mean. Using the reverse function will reverse the order of array. If you are referring to using a hash the sort function would be what you are after.

Expand|Select|Wrap|Line Numbers
  1. open (my $FILEHANDLE, '<', $filename) ||die "Can't open file $!:\n";   
  2. while (my @data = <$FILEHANDLE>) {
  3.     print join("\n>", reverse @data);
  4. }
  5. close ($FILEHANDLE);
Unless, of course I misunderstood what you are wanting to achieve. If that is the case please provide some sample data and the code that you have tried, so that it becomes more clear.

--Kevin
Feb 7 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Perl does not use the term "stack" to describe any of its data types, so your question is not clear. You probably mean an array. But if all you want to do is print the file in reverese order there is no need for an array:

Expand|Select|Wrap|Line Numbers
  1. open (my $FILEHANDLE, '<', $filename) or die "Can't open file: $!\n";   
  2. print reverse <$FILEHANDLE>;
  3. close ($FILEHANDLE); 
Feb 7 '08 #3

P: 74
How would I print out the file with each displayed on a new line?

For example, the file contains:

Hello.
How
are
you
?

I want it to print out like that. However, it prints the file out on one line, like this:

Hello. How are you?

I got it to work when I just displayed the original file without printing it backwards, but I can't figure out how to print the file on new lines when it's printed backwards.

Here is the code I have that prints the original file the correct way (not backwards):

Expand|Select|Wrap|Line Numbers
  1. open(file, "<SCHEDULE") || die "Cannot open file: $!\n";
  2. my $line;
  3. print "<br>ORIGINAL SCHEDULE:\n<br><br>";
  4. while($line = <file>)
  5. {
  6.         print "$line\n<br>";
  7. }
  8. close file;
  9.  
Feb 7 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
One way:

Expand|Select|Wrap|Line Numbers
  1. open(FH,'c:/perl_test/big.txt');
  2. print reverse map{chomp; "$_<br>\n"} <FH>;
  3. close FH;
You have to chomp the lines to get the <br> tag printed before the newline. If that is not a concern you can remove the "chomp;" part from the above code.
Feb 7 '08 #5

P: 74
That worked fine. Now, I am trying to add another feature for fun. This does the same thing as the previous, but it prints the words backwards as well.

Original file:

Hello.
How
are
you
?

Backwards (lines) file:

?
you
are
How
Hello.


Backwards (lines and words) file:

?
uoy
era
woH
.olleH

^ That is what I want to do, but once again I can't get it to print like that! It is all on one line again...

Here's the code that prints it like above but all on one line:

Expand|Select|Wrap|Line Numbers
  1. open(file, "<SCHEDULE") || die "Cannot open file: $!\n";
  2. my $line;
  3. print "<br><br>SCHEDULE (Lines and words backwards):\n<br><br>";
  4. while($line = reverse <file>)  
  5. {
  6.         print "$line <br>\n";
  7. #       print reverse map{"$_ <br>\n"} <file>;
  8. }
  9. close(file);
  10.  
Feb 8 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
the reverse function works in two contexts, list and scalar. In list context it reverses the order of the list, in scalar context it reverses characters in the string.

Expand|Select|Wrap|Line Numbers
  1. open(FH,'file');
  2. print reverse map{chomp; reverse($_)."<br>\n"} <FH>;
  3. close FH;
In some cases this is not good because it reads the whole file into memory. If the file is too big that can cause problems.

Is this school work by any chance?
Feb 8 '08 #7

P: 74
the reverse function works in two contexts, list and scalar. In list context it reverses the order of the list, in scalar context it reverses characters in the string.

Expand|Select|Wrap|Line Numbers
  1. open(FH,'file');
  2. print reverse map{chomp; reverse($_)."<br>\n"} <FH>;
  3. close FH;
In some cases this is not good because it reads the whole file into memory. If the file is too big that can cause problems.

Is this school work by any chance?
Well, yes, it was for extra credit. The actual homework was just to read a file and display its contents.

Thanks for all the help!
Feb 8 '08 #8

eWish
Expert 100+
P: 971
When posting Homework / Classwork on TDSN please make sure you have read the Posting Guidelines.

We are not here to do your homework / classwork for you. But rather here to help guide you so that you can learn. It's to bad that will likely get your extra credit because of else's knowledge and not your own.


Moderator
Feb 8 '08 #9

P: 74
When posting Homework / Classwork on TDSN please make sure you have read the Posting Guidelines.

We are not here to do your homework / classwork for you. But rather here to help guide you so that you can learn. It's to bad that will likely get your extra credit because of else's knowledge and not your own.


Moderator
As you can see in my first post, I didn't ask for code or for anybody to do my homework for me. I simply asked for ideas to do it. I did attempt to do it by myself, and I didn't even use the code that was suggested exactly how it was shown. I modified it to how I wanted it to look and work.

Sorry for possibly breaking your guidelines, but I didn't mean for anyone to do my homework for me as you have assumed.

Hey everybody.

I'm having some difficulty. I know how to make a stack in Perl, but I have no idea how to get it to read from a file then print the contents backwards.

Can anybody give me any ideas on how I might do this?

Thanks in advance!
Feb 8 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.