Register
It is currently Thu Feb 22, 2018 10:33 am

cardinal to ordinal script help please


All times are UTC


Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
 PostPosted: Mon Nov 26, 2007 4:29 pm   
New Member

Joined: Mon Nov 19, 2007 5:15 pm
Posts: 5
After finally figuring out the check book thing ( wanna thank all you agian for helping ) I was wondering is it possible to convert Cardinal to Ordinal? If so how? I have been looking for this for the last week and found nothing on it at all.

Thanks ahead for those that reply i appreciate it.

P.S. Is there a function for it or what i am lost on this completely. Thanks.


Top
 Profile  
 PostPosted: Mon Nov 26, 2007 10:50 pm   
New Member

Joined: Mon Nov 19, 2007 5:15 pm
Posts: 5
Maybe i am missing something but all i need to do is convert 1 2 3 into 1st 2nd and 3rd... so on and so forth so any help would be greatly appreciated. Thanks.


Top
 Profile  
 PostPosted: Tue Nov 27, 2007 3:27 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
I haven't done the script myself, and since I'm guessing this is homework, I don't want to write it for you, but I'll tell you how I would do it.

I would take each number your going to convert and (split) it into 2 parts......

x would equal the original number
y would equal the LAST digit in the number
z would equal all the digits MINUS the last number
do this with CUT and $#
so x = $(z)$(y)

then I would use some if/then statements to convert $(y)
if $y = 1 then $y = "1st"
if $y = 2 then $y ="2nd"

That should get you started I think ;)

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Tue Nov 27, 2007 4:17 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Some other thoughts to add what Dave has already suggested. The rightmost digit is the one that will determine whether you use 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, or 9th.

You may be able to gain some efficiencies by grouping all of the th cases together in a multi-way case statement.

In Bash, the case syntax is similar to the examples provided at http://www.tech-recipes.com/rx/212/bour ... _statement

The discussion at http://www.tech-recipes.com/modules.php ... pic&t=1073 provides further insight into how you may combine the cases.

http://tldp.org/LDP/Bash-Beginners-Guid ... 07_03.html provides additional examples.

More examples: http://www.linuxquestions.org/questions ... ns-557981/
http://www.ibm.com/developerworks/library/l-bash2.html

In this last example http://linuxhelp.blogspot.com/2005/10/1 ... shell.html
you can see the situation where you have the same case being executed for multiple conditions, for instance,

Code:
echo "Do you wish to continue? (y/n)"
read ans

case $ans in
     Y|y) ;;
[Yy][Ee][Ss]) ;;
     N|n) exit ;;
[Nn][Oo]) exit ;;
       *) echo "Invalid command"
esac


I hope these examples provide plenty of ideas, while not directly providing the answer. This logic may be applied to many different programming languages. Quite a few modern programming languages include some form of the case statement. Though the exact syntax differs slightly from language to language, the concepts are often very similar and sometimes identical. This background ought to help get you going. I encourage you to use the Web to do even more research until you locate enough information to help you understand the algorithm and code a great solution. Good luck, and let us know how it comes out!

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Tue Nov 27, 2007 8:53 pm   
Sr. Member
User avatar

Joined: Tue Jul 04, 2006 3:01 am
Posts: 1074
Location: Rochester, NY
masinick wrote:
Some other thoughts to add what Dave has already suggested. The rightmost digit is the one that will determine whether you use 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, or 9th.

That's not entirely true. 12th would be 12nd by that logic, so there's some exceptions to the rule.

Quote:
You may be able to gain some efficiencies by grouping all of the th cases together in a multi-way case statement.

This is probably still true despite the exception noted above

_________________
Ubuntu 8.10 (64-bit), Ubuntu 7.10 (64-bit)
OpenFiler 2.2 (rPath Linux base), Mythbuntu 8.10


Top
 Profile  
 PostPosted: Tue Nov 27, 2007 9:03 pm   
Sr. Member
User avatar

Joined: Tue Jul 04, 2006 3:01 am
Posts: 1074
Location: Rochester, NY
crouse wrote:
I would take each number your going to convert and (split) it into 2 parts......

x would equal the original number
y would equal the LAST digit in the number
z would equal all the digits MINUS the last number
do this with CUT and $#
so x = $(z)$(y)

then I would use some if/then statements to convert $(y)
if $y = 1 then $y = "1st"
if $y = 2 then $y ="2nd"

That should get you started I think ;)


And of course, that's the beauty of development -- there's usually more than one way to do it. ;)

In my case, I'd only split off the last two numbers (if the string is >= 2 digits) and use them to determine what suffix to append to the original string. Using Crouse's notation:
Code:
where w = unused digits > 99
and x' is the final string

x = $(w)$(z)$(y)
# apply case and/or if/else statements here to determine suffix ($(suf))
x' = $(w)$(z)$(y)$(suf)

Bear in mind that x may not have been changed ever, simply its y and z values extracted for testing (possibly to another variable).

An example might be: x = 112
$(w) = 1, $(z) = 1, $(y) = 2
# magic happens here :)
x' = 112th


My suggestion to thinking about the problem is to take a moment, step back, and think about how you can generalize the ordinal numbering system. Are there any rules that you can identify as to what suffix goes on which numbers? Find patterns and code to solve those patterns.

I find it's easiest to clearly understand what you have to do and what exceptions there are (for your various test cases) before I start hacking out code. Little code tests are fine during this stage to understand the language and then you piece the little parts into a larger whole script.

_________________
Ubuntu 8.10 (64-bit), Ubuntu 7.10 (64-bit)
OpenFiler 2.2 (rPath Linux base), Mythbuntu 8.10


Top
 Profile  
 PostPosted: Wed Nov 28, 2007 3:57 pm   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1724
Location: Georgia
i can say this script was kind of fun... i had a feeling it would be... that's why i had to write it :wink:
no... i won't post it.. but i can say, it took 12 lines.

i used one if/then and one case statement YMMV.

and BrionS made a good point about paying attention to the rules, cause there is definitely a set way it is done, mostly you can figure it out by going through a few numbers out loud. i would count to atleast 25... then jump to the hundreds and count to 125... just to get a good idea.

_________________
server: Arch Linux - Kernel 2.6.33-ARCH
Lenovo T60 StinkPad: Arch Linux - Kernel 3.10.10-1-ARCH
VirtualBox 4.2.18 Hosted on MacOS X (10.7.5) Mountain Lion: Arch Linux - Kernel 3.10.10-1-ARCH


Top
 Profile  
 PostPosted: Thu Nov 29, 2007 1:58 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Might be interesting to see what you came up with - once the assignment due date is past, that is.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Nov 29, 2007 2:01 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
http://photomatt.net/scripts/nthnum/

Translate the algorithm to your scripting language and make it your own. This covers what Brions and jbsnakes were mentioning.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Mon Jan 14, 2008 5:20 pm   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1724
Location: Georgia
i think enough time has passed... i can show the way i did it....
Code:
#!/bin/bash

num="${1}"
spcases="${1:${#1}-2:1}"
dig="${1:${#1}-1}"

if [[ ${#num} -gt 1 && $spcases = 1 ]]
then
        addStr="th"
else
        case $dig in
                1)      addStr="st";
                        ;;
                2)      addStr="nd";
                        ;;
                3)      addStr="rd";
                        ;;
                4|5|6|7|8|9|0)  addStr="th";
                        ;;
        esac
fi

echo "${num}${addStr}"

_________________
server: Arch Linux - Kernel 2.6.33-ARCH
Lenovo T60 StinkPad: Arch Linux - Kernel 3.10.10-1-ARCH
VirtualBox 4.2.18 Hosted on MacOS X (10.7.5) Mountain Lion: Arch Linux - Kernel 3.10.10-1-ARCH


Top
 Profile  
 PostPosted: Mon Jan 14, 2008 5:58 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
Very cool jbsnake ;)

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Mon Jan 14, 2008 9:45 pm   
New Member
User avatar

Joined: Tue Sep 11, 2007 6:51 pm
Posts: 22
Location: Nebraska
Here's a possible answer, in Perl

Code:

#! /usr/bin/perl

@sufs =  qw( st nd rd );

while (chomp($input = <STDIN>))
{
  $suffix = "th";  # EVERYTHING is a 'th' except a couple cases
  $lastDig = substr($input, length($input)-1, 1);

  if ( ($lastDig < 4) && !( ($input >3 ) && ($input <21) )) {
    $suffix = $sufs[$lastDig - 1] ;
  }
  print "$input$suffix \n\n";
}

_________________
gNewSense 1.1 (www.gnewsense.org/)
GNUmach-1.3 (www.gnu.org/software/hurd/)


Top
 Profile  
 PostPosted: Tue Jan 15, 2008 1:39 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
The Perl example is actually shorter and it can read from stdin multiple values. That conditional assignment statement in jbsnake's Bash script is something a novice is unlikely to come up with unless they are the A student type that goes way ahead of the class.

jbsnake, why don't you write back and tell the lurkers here what each line of your script does. The vets can figure it out, but the novices may never get it without your explanation.

Colonel Panic, that Perl script also looks like veteran material. The qw function, the compound if, and the digit subtraction are all things that the beginner is unlikely to think of.

Why don't you both take a few minutes and break down your scripts, line by line, so the beginners here can learn from your expertise?

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Tue Jan 15, 2008 3:21 am   
Site Admin
User avatar

Joined: Thu Aug 28, 2003 7:09 pm
Posts: 3815
Location: 42 miles north of Ogdensburg, NY
Colonel Panic, yours fails on 111-113. ;)
Also on 200. ;)

Try this:
Code:
#! /usr/bin/perl

@sufs =  qw( st nd rd );

while (chomp($input = <STDIN>))
{
  $suffix = "th";  # EVERYTHING is a 'th' except a couple cases
  $lastDig = substr($input, length($input)-1, 1);

  if ( ($lastDig > 0 && $lastDig < 4) && !( (($input%100) >3 ) && (($input%100) <21) )) {
        $suffix = $sufs[$lastDig - 1] ;
  }
  print "$input$suffix \n\n";
}


Top
 Profile WWW  
 PostPosted: Tue Jan 15, 2008 10:53 am   
New Member
User avatar

Joined: Tue Sep 11, 2007 6:51 pm
Posts: 22
Location: Nebraska
What, you don't call it the 111st? :P

Thanks for repair!

_________________
gNewSense 1.1 (www.gnewsense.org/)
GNUmach-1.3 (www.gnu.org/software/hurd/)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP