473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HELP! Using a stack to print a file backwards?

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
9 2902
eWish
971 Expert 512MB
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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
971 Expert 512MB
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
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

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

Similar topics

6
by: Edward King | last post by:
Hi! I am trying to achieve the following: I have a number of help pages (in the format help_nn.php where nn=helpid). I want to be able to open a particular help page by calling the function...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Glenn | last post by:
Dynamic help topic links show properly and search brings up links, but when any link is clicked I receive a "Page Cannot be displayed message in the Explorer window and the title bar of Visual...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
27
by: Bruce Dodds | last post by:
I recently started using Access 2003 for the first time. I wanted to pass on some comments about the Help system to Access MVPs who frequent this board. I'm doing this in the hope that some of...
0
by: John Bowman | last post by:
Hi, I'm not certain if this is the proper place to post this, but here it goes... I must be missing something really obvious and am adminttedly a newbie w/ Help systems, I cannot get help to...
3
by: stuart_white_ | last post by:
I've just upgraded from Python 2.3.3 to Python 2.4.2, and, although the new version of Python seems to be running correctly, I can't seem access the help from the interpreter. On Python 2.3.3...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.