Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hey guys, So I again, I come to the forums looking for programming guidance. The language that I'm specifically trying to work with today is Perl.

So pretty much for my class we're being asked to read in from a file, print out to a new file and use a sub routine for the program.

With my knowledge of other programming languages (very limited mind you) I got everything done, except for my sub routine.

My subroutine is supposed to from the read in file take the input for example 21-Nov-1995 and break down the whole numbers into digits and then provide some computations.

so for example 21 = 2 and 1, Nov would be 1 and 1 etc etc.

After reading the chapters this project is supposed to cover I've found not one way to split the digits apart. to be honest the only way I can personally think of based on my past classes would be using a delimiter and even then we haven't covered that in this class yet. So I guess there's a way to do it otherwise?

Any guidance of just how to split up my whole number would be really appreciated (any method or example would do honesty).

Thanks, Shevik

Updated

I understand the splitting part of it, I guess my issue is that fact that it's coming from a file and not <STDIN> this is my first time ever reading from a file. I don't know to access the contents of the file to split them.

My file is called x.txt but the program reads the file name from the keyboard so it could be whatever. The file contains a date, "21-Nov-1995" nothing else. I have gotten to the point to read the file in and I can print the "21-Nov-1995" I guess what I'm failing to pick up is what syntax I'm supposed to use when still trying to manipulate the file for splitting it.

Yes, this is for homework. I can edit it to my liking. As for the book I've read it, I stated I can read in the file in my 3 line of the original post, my apology for the lack of detail. I could go to the instructor and have. With no disrespect for the guy, his English isn't the best. Though anyway the following is what I'm using to open up my file.

do { print "\nEnter an input file name: "; chomp($filename = <STDIN>); open FILE, $filename or die $!; while (<FILE>){ print "$_"; } print "\n\nDo it again, yes (or no)? "; print chomp($answer = lc <STDIN>); } while ($answer eq "yes");

the output does exactly as I'd hope after it's been opened, it prints the date 21-Nov-1995


In reply to Breaking down whole number into digits by shevik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found