Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Code Newbie > Lounge

Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2005, 05:53 AM   #1 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
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
Rocket is offline   Reply With Quote
Old 04-09-2005, 05:58 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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++
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-09-2005, 05:59 AM   #3 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
PERL.
sorry, its late
Rocket is offline   Reply With Quote
Old 04-09-2005, 07:00 AM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 04-09-2005, 07:04 AM   #5 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
name,wage,hours

eg camilla,45,20
Rocket is offline   Reply With Quote
Old 04-09-2005, 07:05 AM   #6 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
oh, its pretty small too,
Rocket is offline   Reply With Quote
Old 04-09-2005, 07:10 AM   #7 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 04-09-2005, 07:14 AM   #8 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
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
Rocket is offline   Reply With Quote
Old 04-09-2005, 07:24 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-09-2005, 08:07 AM   #10 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
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
Rocket is offline   Reply With Quote
Old 04-10-2005, 07:14 AM   #11 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 04-11-2005, 03:23 AM   #12 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
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
Rocket is offline   Reply With Quote
Old 04-11-2005, 08:10 PM   #13 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 04-11-2005, 08:12 PM   #14 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 04-11-2005, 10:47 PM   #15 (permalink)
Rocket
Registered User
 
Join Date: Apr 2005
Posts: 13
Rocket is on a distinguished road
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,
Rocket is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting with php: new line sde Linux / BSD / OS X 9 02-16-2005 05:04 PM
Setting the path for python scripting Intensegutwound All Other Coding Languages 7 11-23-2004 06:32 AM
prepend with shell scripting? sde Linux / BSD / OS X 3 11-08-2004 10:16 AM


All times are GMT -8. The time now is 03:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting