|
 |
|
 |
04-09-2005, 05:53 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
scripting infant
Hi,
I am totally new to this, so i dont mind the abuse that may be hurled at my lame question.
I need to create a script reads a text file, then works out weekly gross and net pay for each person. if the person earns over $1000, they get charged at 50%, and if they earn less its 38%.
The opening and writing to a file im all cool with, but i have nfi where to go from there, lame huh?
Just a gentle nudge in the right direction would probaly suffice, as I would prefer to know what it is I am doing.
TIA
Rocket
|
|
|
04-09-2005, 05:58 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
In what script-language ? php ? *sh ? perl ? or perhaps since you're using alot of calculation based uppon the input, some programming language like C/C++
|
|
|
04-09-2005, 05:59 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
PERL.
sorry, its late
|
|
|
04-09-2005, 07:00 AM
|
#4 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
What's the text file format look like?
Basically (depending on the data and size of it) you can read everything into a hash then loop through and calculate.
-r
|
|
|
04-09-2005, 07:04 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
name,wage,hours
eg camilla,45,20
|
|
|
04-09-2005, 07:05 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
oh, its pretty small too,
|
|
|
04-09-2005, 07:10 AM
|
#7 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
Code:
while(<>) {
chomp;
s/\r//g;
my ($name, $wage, $hours) = split(/\,/);
print "NAME=$name\tWAGE=$wage\tHOURS=$hours\n";
}
So just run like:
Code:
./script_name input_file
So once you have the data in a variable you can do whatever you need. The logic for looking at the wage > 1000 should be pretty straight forward.
-r
|
|
|
04-09-2005, 07:14 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
thanx heaps, now im pointed in the right direction, just have to work out what all that did, i think i got it
thanx again
|
|
|
04-09-2005, 07:24 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
In short psudocode:
Code:
while theres lines in the file
read one
replace '\r' with nothing (making sure every line ends with a '\n')
split the line into three containers with ',' as the seperation char.
print out whats filled into the containers
|
|
|
04-09-2005, 08:07 AM
|
#10 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
Hey,
Just one more thing, it seems to only be taking the last name and numbers from the file, and i cant work out for the life of me why...
otehr than that, all seems to be working fine.
thanx yet again
|
|
|
04-10-2005, 07:14 AM
|
#11 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
How are the names formatted? (I assume you're saying that it's missing the first name)
Quote:
|
replace '\r' with nothing (making sure every line ends with a '\n')
|
Actually chomp is removing the '\n' so $_ is a clean line.
-r
|
|
|
04-11-2005, 03:23 AM
|
#12 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
No, i mean it is onll taking one name from the file.
it is in following format:
pete,50,20
mary,34,23
dave,98,34
sue,23,34
there are only four names, and it only seems to tally the last name and numbers on the list.
I kept playing and plaiying, gpt most of the other stuff sorted, very messy indeed.
but no matter what i did, i couldnt work out how to make it tally all names in the file, here is my script so far, and thanx for the help,
Code:
open NAME, 'namedate.txt' || die "cannot open namedate.txt: $!\n";
open NAME2, '>nam2.txt' || die "cannot open nam2.txt: $!\n";
chomp;
while(<NAME>) {
@num=($name, $wage, $hours) = split(/\,/);
$total=($wage * $hours);
}
if ($total>1000) {
$taxed=($total/2)
}
elsif ($total<1000){
$taxed=($total*.38)
}
$tax=($total-$taxed);
print NAME2 "NAME= $name\n\nWAGE=\$ $wage\tHOURS= $hours\n NET=\$$total\tGROSS=\$$taxed\ttax payed=\$$tax";
close NAME;
close NAME2;
the @num array was originally a my statement, but i was to lame to even get that going, hehe
thanx again
|
|
|
04-11-2005, 08:10 PM
|
#13 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
Well, for good paranioa you should keep chomp; and the s/\r//g; line in there just after the while statement. They clean up the input line.
@num - yeah remove that.
If you run my original script, does it print all the names and wage/hours data from the file?
-r
|
|
|
04-11-2005, 08:12 PM
|
#14 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
 Just looked at the code again and noticed the part I missed. (must be late)
Note your if logic with $total. You placed it outside the while loop structure. You have to put it before the closing } of while so it executes on each line of the file.
-r
|
|
|
04-11-2005, 10:47 PM
|
#15 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 13
|
No, i couldnt get it to print anything to output file, my fault im sure,
I placed the if statement inside the while loop, and seem to be getting the first name only now, a little bit closer, and I am kinda learning something,
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:20 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|