a math related question

hello:

First I know the rules on Homework related questions,

I wrote my script, but I cannot seem to figure out how to do one math problem.

How do I take a zip code and seperate the idvidual digits?

I used the modulus expression and divided the number by 10 ^ n
but that only worked for the last digit.

any MATH revelations would be great.

I dont really understand what it is you are asking for. However, to do math in shell, you can use the bc command or you can pass in your values to Awk and let Awk do the work for you. These are probably your best bet for floating point math calculations. See man awk and man bc for more information. For greater information regarding Awk, see this site --> Awk GNU Documentation

thanks for your reponse,

sorry for the bad description, but i am trying to keep within the integrety of the site's homework policy. I really am stuck though.

I am using perl and need to take 11368 and make it
1
1
3
6
8

each digit asigned to its own variable.

thanks

kevin

Im not really a Perl guy (wish I was!), but if the context of the variable is a string, then you could split the string into its component parts. Try a substring or split. Continuing with your modulus approach, divide by 10 to move the decimal point to the left.

How about using a regular expression (in this case any single character) to create an array using split. I dont really write any Perl (just hack around sometimes), so I am not really sure if this will work!

@a = split /./, $string;

From a math standpoint, suppose the zip code is 12345 and you want to isolate the that 3. Drop the two right most digits by dividing by 10^2. 12345 / 100 = 123. Now use your modulus trick.

awk:

echo "12345" | awk '{ for(i=1;i<6;i++) printf "%s \n", substr($1,i,1) }'

:smiley: dear jim ...
i can understand the awk command you wrote but I am little soft on the substring part.. any chance you could explain it..
thanx moxxx68:D

Perl code:

$code = 11368;
@digit = ();

for ($i = 0; $i < length($code); $i++)
{
    $digit[$i] = substr($code, $i, 1);
    print $digit[$i]."\n";
}

The substr() function takes 3 arguments, the string, offset (position, start from 0), and length. For example, substr("Hello", 1, 2) will return the string "el". The details can be obtained from the man page "man perlfunc".

:smiley: will look it up>> but i have one more question! how do i access the perl tutorial man pages.. I have tried everything and learning some perl would help with this type of issue.. any suggestions on how i can access this tutorial or any of the information on perl itself,, I can't seem to make heads ot tails of trying to get into the man pages of the perl section.. gnu/linux (fediora core 2 vs 2.6.8-1.521).
thanx moxxx68..:cool:

Typing "man perl" will give you a list of the various other perl manpages that are available.

"man perlintro" is as good a place to start as any.

Searching Google for Perl Tutorials will give you 2,040,000 results.

Peace
ZB