Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Breaking down whole number into digits

by shevik (Initiate)
on Nov 29, 2012 at 19:34 UTC ( [id://1006344]=perlquestion: print w/replies, xml ) Need Help??

shevik has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Breaking down whole number into digits
by roboticus (Chancellor) on Nov 29, 2012 at 19:44 UTC

    shevik:

    There are various ways to split or unpack a string into individual characters. 8^)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Breaking down whole number into digits
by Kenosis (Priest) on Nov 29, 2012 at 19:48 UTC
Re: Breaking down whole number into digits
by pvaldes (Chaplain) on Nov 29, 2012 at 21:25 UTC

    "... this is my first time ever reading from a file

    Well, you have a teacher and this is a pretty basic stuff, there are some particularities about how to read a file that you need to know, ask him/her before to truncate something important.

    Any guidance...

    Yes, if your handbook is so poor that forget to tell you about how to open a file, at least use perldoc to examine the function open

    My file is called x.txt but the program reads the file name from the keyboard so it could be whatever

    Well, you wrote THIS program for your homework, isn't? You can change your own program

    Please, show us a little code

Re: Breaking down whole number into digits
by aaron_baugher (Curate) on Nov 29, 2012 at 21:49 UTC

    Since you want "Nov" to be translated into "1 1", I'm guessing that you want to average the lowercase ordinal values of the first and third letters, find the difference between that and the ordinal value of the second letter, express that in binary, and then split that into digits. Here you go; hope it helps:

    #!/usr/bin/env perl use Modern::Perl; my @n = split '', lc('Nov'); my @arr = split '', sprintf "%0b", (abs((ord($n[0])+ord($n[2]))/2 - ord($n[1]))); say join ' ', @arr;

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: Breaking down whole number into digits
by ww (Archbishop) on Nov 29, 2012 at 21:25 UTC

    Ignoring (because you've given us no hint of how you plan to translate "Nov" to 11), you can, of course, use a regex to separate a number like "11" into separate digits:

    >perl -E "my $foo=11; if ($foo =~ /(\d{1})(\d{1})/) { say \"$1 \t $2\" +;}" 1 1

    Note that this is NOT a recommended approach since it doesn't deal with "1995" or with the ...say, second day of a month.

Re: Breaking down whole number into digits
by Anonymous Monk on Nov 30, 2012 at 02:47 UTC

    Hi,

    Look at hashes for translating the month into digits.

    substr is another way to break down strings into their component parts.

    J.C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1006344]
Approved by Kenosis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-03-19 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found