Creating an array through a loop

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Gautami
Posts: 35
Joined: Tue Aug 06, 2013 5:25 am

Creating an array through a loop

Post by Gautami » Tue Aug 06, 2013 1:36 pm

Hi all,
I am very very new to programming and have found the forums here to be an enormous source of help for me. So thank you so much!

I have been trying to program an application for some weeks now with the help of the forums but I have run into some problems that i am struggling to resolve. I hope maybe you guys could help shed some light on it.

The essence of what I am trying to do is take in a folder, create a loop to md5 the files in it and then store the names and md5 sums into a variable. Next I will duplicate the folder and repeat the process to md5 and store the information into a second variable. Then I will compare both the variables to verify that they are the same. I will also be displaying the contents of the variable into a log file.

This is my current code. Somehow I am unable to include the file name into the variable and it also takes very long time (30-40 seconds..) to display the output and the screen freezes as well. Please take a look and tell me how I may resolve this. Thank you.

-------------------------------
global gLogData
global HashValue
on mouseUp
answer folder ("Select your folder containing the Database")

put it into fld 1
put logData(fld 1) into tLog[1] -------- logData is a function that will timestamp the content in fld 1 and store into an array

answer "Create a duplicate of selected folder"
answer folder ("Select destination to copy the Database to")
put it into fld 2
put cr & logData(fld 2) into tLog[2]

combine tLog using return
put tLog into fld 3

local tDefault, tItems
set the defaultFolder to fld 1
put empty into field 4

put the files into tItems
filter tItems without "."

repeat for each line xLine in tItems
put cr & "name=" & quote & xLine & quote into HashValue[xLine]
put cr & "checksum=" & quote & fileDigest(the defaultFolder & "/" & xLine) & quote & return into HashValue[xLine]
end repeat
combine HashValue using return
put HashValue into fld 4
end mouseUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Creating an array through a loop

Post by dunbarx » Tue Aug 06, 2013 2:08 pm

HI..

I only checked your script for compliance, since I do not have the other functions you mentioned. So I simply substituted short strings for whatever would have been returned by those functions.

Nothing untoward, and the process proceeded instantaneously. Is it possible that the functions themselves are taking the extra time?

Craig Newman

Klaus
Posts: 14220
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating an array through a loop

Post by Klaus » Tue Aug 06, 2013 2:13 pm

Hi Gautami,

1. welcome to the forum! :-)

2. you are OVERWRITING the filename with the checksum in your repeat loop:
...
put cr & "name=" & quote & xLine & quote into HashValue[xLine]
## BAD:
## put cr & "checksum=" & quote & fileDigest(the defaultFolder & "/" & xLine) & quote & return INTO HashValue[xLine]
## HashValue[xLine] already has a value in it - the filename, but this line will overwrite this

## Good:
put cr & "checksum=" & quote & fileDigest(the defaultFolder & "/" & xLine) & quote & return AFTER HashValue[xLine]
...

Depending on the size of your files, md5digest will take some time, I think.


Best

Klaus

Gautami
Posts: 35
Joined: Tue Aug 06, 2013 5:25 am

Re: Creating an array through a loop

Post by Gautami » Tue Aug 06, 2013 3:40 pm

Hi Klaus and Dunbarx,

Oh I see! I was worried because the function returned 1/2 the input before freezing for a while,
but now I realise the error. I will have to do something abt the function, at least put up a warning in future, I suppose.

Thank you!

Gautami

Post Reply