Day 2: Arguments

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
So far we have been outputting to the shell, hello world and the osnn.net lines. However, there has to be a way that we can specify what we want it to do by checking the argument we get sent when we start the program.

Task 2-1: Create a program that prints all the arguments passed to it
Task 2-2: Modify the above program to sort them alphabetically first
Task 2-3: Find all duplicate arguments and remove them
Task 2-4: Using your knowledge about arguments. Create a program that depending on what the argument passed is will ask for either of the following:
Arg 1: Your name and birthday, and outputs how old they are in days.
Arg 2: Random line of text, to be printed on new lines
Arg 3: Print hello world.

So "program 2" would ask me for a random line of text and print it out on new lines.

If no argument is passed it should print the list of options as well as the name of the current program, and how it should be used.

Good luck. I will post my solution to Day 1 tomorrow.
 
Reviving the thread using VBScipt/WSH:
Task 2-1: Create a program that prints all the arguments passed to it
Task 2-2: Modify the above program to sort them alphabetically first
Task 2-3: Find all duplicate arguments and remove them
Well, VBScript has no built in sort function, so I used my own quick sort function.

edit: this is using a straight string/ascii sort so numbers aren't sorted correctly. I could correct it, but I'm too lazy right now ---> okay, this is fixed now
Code:
if wscript.arguments.count=0 then
	wscript.echo "No Arguements Passed"
	wscript.quit
else
	arrCount=0
	dim arrList()
	for i = 0 to wscript.arguments.count-1
		strArg = wscript.arguments(i)
		if isNumeric(strArg) then strArg = cInt(strArg)
		strLine = "," & join(ArrList,",") & ","
		if inStr(strLine,"," & strArg & ",")=0 then
			redim preserve arrList(arrCount)
			arrList(arrCount) = strArg
			arrCount = arrCount+1
		end if
	next
End If

if uBound(arrList) > 0 then call QuickSort(arrList,0,uBound(arrList))

for i = 0 to uBound(arrList)
	wscript.echo arrList(i)
next


'/*************************
Sub QuickSort(arrData,intLow,intHigh)

	if intHigh - intLow = 1 then
		if arrData(intLow) > arrData(intHigh) then
			strTemp=arrData(intLow)
			arrData(intLow) = arrData(intHigh)
			arrData(intHigh) = strTemp
		End If
	End If

	intPivot = arrData(int((intLow + intHigh) / 2))
	arrData(int((intLow + intHigh) / 2)) = arrData(intLow)
	arrData(intLow) = intPivot
	intLowTemp = intLow + 1
	intHighTemp = intHigh

	do
		while intLowTemp < intHighTemp and arrData(intLowTemp) <= intPivot
			intLowTemp = intLowTemp + 1
		wend

		while arrData(intHighTemp) > intPivot
			intHighTemp = intHighTemp - 1
		wend

		if intLowTemp < intHighTemp then
			strTemp = arrData(intLowTemp)
			arrData(intLowTemp) = arrData(intHighTemp)
			arrData(intHighTemp) = strTemp
		End If

	loop while intLowTemp < intHighTemp
  
	arrData(intLow) = arrData(intHighTemp)
	arrData(intHighTemp) = intPivot
  
	if intLow < (intHighTemp - 1) then Call QuickSort(arrData,intLow,intHighTemp-1)

	if intHighTemp + 1 < intHigh then Call QuickSort(arrData,intHighTemp+1,intHigh)

End Sub

Task 2-4: Using your knowledge about arguments. Create a program that depending on what the argument passed is will ask for either of the following:
Arg 1: Your name and birthday, and outputs how old they are in days.
Arg 2: Random line of text, to be printed on new lines
Arg 3: Print hello world.

So "program 2" would ask me for a random line of text and print it out on new lines.

If no argument is passed it should print the list of options as well as the name of the current program, and how it should be used.

Good luck. I will post my solution to Day 1 tomorrow.
I'll add this last one tomorrow (maybe) if I get around to it.
 
Last edited:
Ruby

Code:
#!/usr/bin/env ruby
#
# Task 1: 
#  Create a program that prints all the arguments passed to it

puts ARGV.join(' ')

Code:
#!/usr/bin/env ruby
#
# Task 2: 
#  Modify the above program to sort them alphabetically first

puts ARGV.sort.join(' ')

Code:
#!/usr/bin/env ruby
#
# Task 3: 
#  Find all duplicate arguments and remove them

puts ARGV.uniq.join(' ')

Code:
#!/usr/bin/env ruby
#
# Task 4: 
#  Create a program that depending on what the argument passed is will ask for either of the following:
#   Arg 1: Your name and birthday, and outputs how old they are in days.
#   Arg 2: Random line of text, to be printed on new lines
#   Arg 3: Print hello world.

require 'time'

SECONDS_PER_DAY = 86_400

case ARGV.first.to_i
when 1
  print "Enter your birthday yyyy/mm/dd: "
  bday = Time.parse($stdin.readline.chomp)
  days = (Time.now.utc - bday.utc).to_i / SECONDS_PER_DAY
  puts ("You are #{days} days old.")
when 2
  print "Gimme some random words: "
  puts $stdin.readline.chomp.split(' ').join("\n")
when 3
  puts "hello world"
else
  puts <<-EOM
Usage: task4.rb [option]

Options:
  1   - Will prompt for birthdate and give your age in days
  2   - Will take a random string of words and print them on new lines
  3   - Will print out "hello world"
EOM
end
 
Reminds me of APL circa 1975. A program langauge designed so you can write the entire program in one line of code.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back