Jump to content

Newbie coder looking for help with arrays


The_Lorax
 Share

Recommended Posts

Hello,

I have a one dimentional array of varying size that contains the following data:

name,time stamp

for example:

Bob,13.00

Anne,14:00

Bill,15:00

Bob,16:00

I want to search the array for multiple entries with the same name. If a person has 10 or more entries in the array I want to run an event.

Is there a function to do this or will I need to try and code my own?

I'm fairly new to AutoIt and programming and I'm not sure how to proceed.

Thanks for your help!

Link to comment
Share on other sites

I want to search the array for multiple entries with the same name. If a person has 10 or more entries in the array I want to run an event.

Is there a function to do this or will I need to try and code my own?

I'm fairly new to AutoIt and programming and I'm not sure how to proceed.

O.K. first you should sort the array (_ArraySort). You will then have something like this:

Bob, 11:00

Bob, 12:00

Bob, 13:00

Anne,09:00

Anne,10:00

Anne,13:00

To find multiple entries for Bob, Anne, etc. just loop through the array and count the number of occurances for each person. If the count exceeds 10, then trigger some action.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

The thing making this hard is:

1. the array will vary in size.

2. I don't know the names of the people that are going to be placed in the array.

I'm really stumped about how to check if the same name appears in the list 6 times or more. it would be easy If I knew the names in advance - i'd just have a loop with a ton of if... then statements.

Link to comment
Share on other sites

The thing making this hard is:

1. the array will vary in size.

2. I don't know the names of the people that are going to be placed in the array.

No problem.

1.) Ubound() will give you the size of an array.

2.) If you only need the information, that any name occurs serveral times in the array, you could do this:

const $MAXCOUNT = 10
$name = $array[0][0];   [n][0] == name , [n][1] == "time"
$occurance = 0

for $n = 1 to Ubound($array)
    if $array[$n][0] = $name then
         $occurance = $occurance +1
         if $occurance >= $MAXCOUNT then
            do_something($name,$occurance)
         endif
    else
         $name = $array[$n][0]
         $occurance = 0
    endif
next

func do_something($name,$howoften)
    msgbox(0,"INFO", "Name : " & $name & " How often: " & $howoften)
endfunc

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

No problem.

1.) Ubound() will give you the size of an array.

2.) If you only need the information, that any name occurs serveral times in the array, you could do this:

const $MAXCOUNT = 10
$name = $array[0][0];    [n][0] == name , [n][1] == "time"
$occurance = 0

for $n = 1 to Ubound($array)
    if $array[$n][0] = $name then
         $occurance = $occurance +1
         if $occurance >= $MAXCOUNT then
            do_something($name,$occurance)
         endif
    else
         $name = $array[$n][0]
         $occurance = 0
    endif
next

func do_something($name,$howoften)
    msgbox(0,"INFO", "Name : " & $name & " How often: " & $howoften)
endfunc

Cheers

Kurt

<{POST_SNAPBACK}>

The user said 1 dimensional array, this would require splitting the strings

with some minor changes this can be done with 1 dimensional array

for would be from 0 to ubound($array) - 1

$name would have to be stringmid($array[$n],1,stringinstr($array[$n],",") - 1)

or something thereof

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

The user said 1 dimensional array, this would require splitting the strings

Yep, you're right. I guess I read over that. But, as you said, some minor changes will do the trick. Here we go....

const $MAXCOUNT = 10
const $DELIMITER = ","

$name = get_name($array[0])
$occurance = 0

for $n = 1 to Ubound($array)-1
    if get_name($array[$n]) = $name then
         $occurance = $occurance +1
         if $occurance >= $MAXCOUNT then
            do_something($name,$occurance)
         endif
    else
         $name = get_name($array[$n])
         $occurance = 0
    endif
next

func do_something($name,$howoften)
    msgbox(0,"INFO", "Name : " & $name & " How often: " & $howoften)
endfunc

func get_name($text)
    local $items = StringSplit($text,$DELIMITER)
    return $items[1]
endfunc

func get_time($text)
    local $items = StringSplit($text,$DELIMITER)
    return $items[2]
endfunc

Instead of using $DELIMITER as a global variable, we could also add a param to the functions.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

I modified your code /dev /null to work with a one dimentional array. I used this script to test it:

const $MAXCOUNT = 5
$occurance = 0
dim $array[10]
$array[0] = "Bob,date"
$array[1] = "Sue,date"
$array[2] = "Bob,date"
$array[3] = "Sue,date"
$array[4] = "Bob,date"
$array[5] = "Sue,date"
$array[6] = "Bob,date"
$array[7] = "Sue,date"
$array[8] = "Bob,date"
$array[9] = "Sue,date"


$name = stringmid($array[0],1,stringinstr($array[0],",") - 1)
for $n = 1 to Ubound($array)-1
    $currentstring = stringmid($array[$n],1,stringinstr($array[$n],",")-1)
     if  $currentstring = $name then
        $occurance = $occurance +1
        if $occurance >= $MAXCOUNT then
            do_something($name,$occurance)
        endif
    else
         $name = stringmid($array[$n],1,stringinstr($array[$n],",")-1)
         $occurance = 0
    endif
next

func do_something($name,$howoften)
    msgbox(0,"INFO", "Name : " & $name & " How often: " & $howoften)
endfunc

Currently the code will only add +1 to occurance if the array directly after array[n] has an identical name. I think I'm going to need to include a nested loop in here to check all of them. It's 11pm, I can't concentrate so i'm going to go make some dinner and think about how it could be done.

Thanks for your help so far ;)

Link to comment
Share on other sites

ignore that last post.. just realised I forgot to add an _ArraySort!

I'll go try that now. should work!

<{POST_SNAPBACK}>

As you said, add _ArraySort() and it will work. ;)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...