AutoHotkey Community

It is currently May 26th, 2012, 7:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6 ... 28  Next
Author Message
 Post subject: Re: fileexist
PostPosted: June 1st, 2009, 12:48 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
tinku99 wrote:
The task doesn't saying anything about "differentiating". It just happens to ask for the existence of a file and a directory.
Yes, it asks for a file and a directory: two different things, thus the code must differentiate between the two, which the current AHK example does not. I've noticed that many (most?) of the rosetta code tasks are poorly worded; but this sentence is unambiguous:
Quote:
In this task, the job is to verify that a file called "input.txt" and the directory called "docs" exist.
It does not ask to verify that a "file or directory" called input.txt exists; it asks for a file. Unfortunately, coding this in AHK requires multiple steps. :?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 9:21 am 
some other examples don´t differentiate either, e.g. http://rosettacode.org/wiki/File_Exists#DOS_Batch_File
So i´m not shure if it´s really demanded, but
if you exchange the example, please replace the ternary operators by full blown if-clauses. It will be much more understandable that way.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 3:46 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Laszlo noted that most of the 99 bottles of beer examples are wrong also. I'm sure Rosetta code doesn't have the manpower to validate all the code examples (or even a fraction of them). Also, part of the site's purpose is to see how different languages attempt to solve a task, even if that task can't be solved in a given language.

So my question is more philosophical. Is it better to show a readable example in the spirit of the task, even if that example doesn’t meet all requirements, or should the problem be explicitly solved, even if it requires longer, less-readable code to fulfill every detail?

Right now, I’m leaning towards solving the problems in detail. If we assume the details are there for a reason (not necessarily true) and if AHK can fulfill the details, I see more value in being sticklers… but I’m open to other ideas. :?:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 5:25 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
jaco0646 wrote:
Laszlo noted that most of the 99 bottles of beer examples are wrong also.


Eek. Was mine incorrect also? I was sure I tested it to work correctly.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 5:42 pm 
After rereading the task, i lost my doubts, the Batch- and the AHK-example are both wrong. So at least the ahk-version should be exchanged.

IMHO we should explicitly solve all given tasks and meet all requirements we can.
it is probably neither good for rosetta nor for ahk if we show only the "good" sides of the programming language.
especially if we skip requirements for better readability, people might get the impression, that certain tasks are not doable, when they are in fact only a little bit more complex.

maybe this is more readable and still correct?
Code:
ShowFileExist("input.txt")
ShowFileExist("\input.txt")
ShowFolderExist("docs")
ShowFolderExist("\docs")
return

ShowFileExist(file)
{
   if ( FileExist(file)  && !InStr(FileExist(file), "D"))
      MsgBox, file: %file% exists.
   else
      MsgBox, file: %file% does NOT exist.
   return
}

ShowFolderExist(folder)
{
   if InStr(FileExist(folder), "D")
         MsgBox, folder: %folder% exists.
   else
      MsgBox, folder: %folder% does NOT exist.
   return
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 7:01 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
That code looks good to me. I've posted it on Rosetta with a note about the FileExist() function.

@sinkfaze
I didn't test your example for accuracy. I think Laszlo's main complaint was readability.

<pontificating>
Given the choice between two accurate alternatives, I like to post examples that play to AHK's strengths. For GUI creation, AHK has other languages over a barrel; so I like to show examples of simple little GUIs. Also, I believe other languages use the command line to display (or "print") text because that's the easiest method for them. No one does this with AHK. The most common way we display text is with MsgBox, followed by custom GUIs. I think we should try to show examples that portray how AHK would actually be used to complete tasks.

I’m indecisive about posting code for many of the tasks, because I try to juggle several priorities.

1. Accurate – is every detail of the task completed?
2. Readable – is the code unnecessarily concise?
3. Representative – does the code portray typical usage?

Whenever possible, AHK examples should meet all three of these ideals; otherwise I try to accomplish them in order.
</pontificating>


Last edited by jaco0646 on June 4th, 2009, 7:20 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 9:02 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
Well on that note, here's an AHK solution for Divide By Zero Detection that can be checked for optimization, although I'm not sure how you quantify their requirements (not checking the denominator for a zero, is detection required before or after the process is complete, etc):

Code:
ZeroDiv(num1,num2) {

if (num1/num2)
  MsgBox % num1/num2
else
  MsgBox, 48, Warning, The result is not valid (Divide By Zero).

}

!x::ZeroDiv(24,4)
!y::ZeroDiv(8,0)

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 10:00 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
I think checking the output as you've shown is the best AHK can do. A more concise example is
Code:
Loop,2
 MsgBox,% 1/(A_Index-1) ? true:false
... but is that "unnecessarily concise?" :lol: Here's where I'm indecisive. :P

P.S. Shouldn't 0/0 be a valid operation?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: divide by zero
PostPosted: June 2nd, 2009, 6:01 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
Code:
ZeroDiv(num1,num2) {
if ((num1/num2) != "")
  MsgBox % num1/num2
else
  MsgBox, 48, Warning, The result is not valid (Divide By Zero).
}
ZeroDiv(0, 3)
ZeroDiv(3, 0)

You have to check for an empty string not just a false value, to make sure a zero numerator does not give a warning.

You don't have to worry about optimizing these silly examples.

I wouldn't worry about 0/0 too much...
Unless you want to get into field theory


Last edited by tinku99 on June 2nd, 2009, 9:37 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject: bubblesort
PostPosted: June 2nd, 2009, 9:20 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
Another version of bubblesort here


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 3:25 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
jaco0646 wrote:
A more concise example is
Code:
Loop,2
 MsgBox,% 1/(A_Index-1) ? true:false
... but is that "unnecessarily concise?" :lol: Here's where I'm indecisive. :P


I was just throwing the code together to get an answer but after seeing tinku99 wrap the code into functions for many of these answers I'm becoming partial to that idea since a potential tester can enter their own values into the function call to confirm it works. Particularly in the case of something like dividing by zero, where a zero in the numerator can be tested to ensure that it does not error the same way that a zero in the denominator does.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 3:47 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Strictly speaking, the ZeroDiv function above is still incorrect: if num1 or num2 is non-numeric, the result of the division is still empty, but there was no division by zero. You just have to check if num2=0.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 3:58 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Laszlo wrote:
You just have to check if num2=0.
The task explicitly forbids this.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 4:19 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I see. But this task makes no sense, since most interpreted languages check the divisor internally, anyway. If you still want to play along, first you have to check num1 and num2 “if var is number”.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 2nd, 2009, 4:43 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
I had initially created a separate thread for this, but in the interest of not cluttering the forum, will delete that other thread.

I added a category on rosettacode for tasks that seem so obvious to me as an autohotkey user, but aren't traditionally supported well in programming languages: AutoHotkey_Originated.
It was also a good way of keep track of what we add, besides translating the tasks already on rosettacode.
Consider posting any new tasks here, before posting on the wiki.
If you do add a task, use this wiki template:
Code:
{{task}}...task description here...
__TOC__
=={{header|AutoHotkey}}==
<lang AutoHotkey>
... autohotkey example code here...
</lang>
[[Category:AutoHotkey_Originated]]


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 11 guests


You can post new topics in this forum
You can 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

Search for:
Powered by phpBB® Forum Software © phpBB Group