Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #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


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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (3)
    As of 2018-05-26 18:19 GMT
    Find Nodes?
      Voting Booth?