Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Breaking down whole number into digits

by shevik (Initiate)
on Nov 29, 2012 at 19:34 UTC ( #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


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


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


    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 (Bishop) 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


    Look at hashes for translating the month into digits.

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006344]
Approved by Kenosis
[haukex]: But I was just using it as an author test anyway
[Corion]: haukex: Aaah - I thought you were still running these tests on every machine, but you only run these as author or Devel::Cover tests
[Corion]: haukex: Yeah, I think back then I used Test::Inline, which used a pod parser that was going through some changes and I didn't want to cater for all the various versions and thus stopped testing the Pod completely
[choroba]: I usually do this with presentations
[Corion]: But now I think statically (re)generating the Pod tests is a saner approach, and likely I'll regenerate the tests either in Makefile.PL or from xt/ but have them live below t/
[choroba]: I keep the snippets in files of their own, and use a Makefile to syntax highlight them and insert them into slides, while also running them and inserting the output if required
[Corion]: choroba: Ooooh - I didn't think of that! I write my presentations as POD and if it "roughly" looks like Perl code, I should also syntax-check that...
[haukex]: Yes sorry I don't run them all the time, my POD tests are only run as author tests (and are excluded when I'm using Devel::Cover)
[Corion]: choroba: Hmm - no, I keep the snippets inline, but as my framework also has support for capturing output etc., maybe I should do the same...
[Corion]: haukex: Yes, that approach is sane, and it heals the fragility of Pod parsers in a nice way while still syntax-checking stuff

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-02-27 12:25 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.