Finding duplicates in an array and adding figures... Puzzle

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Tue May 17, 2022 12:30 pm

Hi all,

I have battled over this for a while and it is time to ask for assistance.

Scenario is this.
  • I have an Array that contains names
  • I have another Array that contains numbers
  • Both arrays have the same amount of fields and they match in order.
So as a (simplified) example:

Code: Select all

ARR1 ARR2
Chris 5.400
Steve 6.230
Phil 0.501
Steve 2.001
Steve 3.000
Phil 1.00
The arrays are not too big (and believe me it has taken weeks to get this far parsing some very odd data)

So what I need to do is end up with a file (or another array) that contains one of each name and the total numbers like this:

Code: Select all

Chris 5.400
Steve 11.231
Phil 1.501
Bearing in mind that there are 200+ names that I do not know in advance.

I have tried using regex and almost got there but failed. I think I just need a bit of guidance here to what seems like an easy thing to solve on paper but in code it is beating me.

Can anyone offer any ideas?

Thank you in advance of course.
Phil Pendlebury - Linktree

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Dorian (MJT support) » Tue May 17, 2022 1:59 pm

It might be the ASD in me overthinking this (I'm sure some here can find a simpler way) but I was able to do this by building an ini file.

Code: Select all

deletefile>d:\Names.ini
writeln>d:\Names.ini,,

//Create Sample Name Array
ArrayDim>Names,4
Let>Names_1=Fred
Let>Names_2=Sally
Let>Names_3=John
Let>Names_4=Fred

//Create Sample Number Array
ArrayDim>Numbers,4
Let>Numbers_1=1
Let>Numbers_2=2
Let>Numbers_3=3
Let>Numbers_4=4

//Create Blank ini entries and find the number of unique names
ArrayCount>Names,NameCount
Let>TotalNameCount=NameCount
Let>ArrayLoop=0
Let>NameString=

Repeat>ArrayLoop
  let>ArrayLoop=ArrayLoop+1
  Let>Name=Names_%ArrayLoop%
  editinifile>d:\Names.ini,List,Name,0
  pos>Name,NameString,1,AlreadyExists,
  If>AlreadyExists=0
    Let>NameString=%NameString%%Name%~
    else
    Let>TotalNameCount=TotalNameCount-1
  Endif
Until>ArrayLoop,NameCount


//Edit the ini file adding the numbers as we go
Let>ArrayLoop=0
Repeat>ArrayLoop
  let>ArrayLoop=ArrayLoop+1
  Let>Name=Names_%ArrayLoop%
  ReadIniFile>d:\Names.ini,List,Name,IniNumber
  let>Number=Numbers_%ArrayLoop%
  let>NewNumber=Number+IniNumber
  editinifile>d:\Names.ini,List,Name,NewNumber
Until>ArrayLoop,NameCount

//Output the results of each unique name
Let>ArrayLoop=0
Repeat>ArrayLoop
  let>ArrayLoop=ArrayLoop+1
  Let>Name=Names_%ArrayLoop%
  ReadIniFile>d:\Names.ini,List,Name,IniNumber
  MessageModal>%Name% : %IniNumber%
Until>ArrayLoop,TotalNameCount


executefile>d:\Names.ini
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Tue May 17, 2022 2:26 pm

Quite ingenious Dorian, and funny that it crossed my mind if only we had some of the ini file features in regular file reads etc. I will have mess with this and let you know how I get on. Thank you for your time.
Phil Pendlebury - Linktree

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Dorian (MJT support) » Tue May 17, 2022 2:59 pm

You're very welcome.

For those who don't run the script, this is the output :

Code: Select all

[List]
Fred=5
Sally=2
John=3
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
JRL
Automation Wizard
Posts: 3460
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Finding duplicates in an array and adding figures... Puzzle

Post by JRL » Tue May 17, 2022 3:44 pm

Here's my take:
Using arrays, no files. Take the list and place it into an array with Separate>. Sort the array using ArraySort> then write to a new list each time a new name arrives summing the numbers if the name repeats.

Started the repeat at 1 to skip over the blank line created by LabelToVar>

Code: Select all

/*
List:
Chris 5.400
Steve 6.230
Phil 0.501
Steve 2.001
Steve 3.000
Phil 1.00
*/

LabelToVar>List,vList
Separate>vList,crlf,combined

ArraySort>Combined

Let>MergedList=
Let>BB=1
Repeat>BB
  Add>BB,1
  Let>value=Combined_%BB%
  Separate>value, ,item
  If>BB=2
    Let>CurrName=item_1
    Let>CurrSum=0
  EndIf
  If>CurrName=item_1
    Let>CurrSum=CurrSum+item_2
  Else
    Let>MergedList=%MergedList%%CurrName% %CurrSum%%crlf%
    Let>CurrSum=item_2
  EndIf
    Let>CurrName=item_1
Until>BB={%combined_count%+1}

MDL>MergedList

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Tue May 17, 2022 6:21 pm

Thanks guys, JRL I think you have it.

I think I have fried my brain though as I cannot get my array into that code.
I have an Array let's call it "ARRNICE"
Which contains the list of names and figures.

Actually I can make this array contain the figures and the names OR the names and the figures as it is made up of two other arrays.

So this also means that the separator can be anything (except a space really as there are spaces in the names).

So ARRNICE looks like this:

Code: Select all

Mister Something=1.2456
Mrs Somehne Else=12.5
Mister Something=2.2456
Mister Something=2.2456
Uncle Who=45.2
I prefer it to look like this:

Code: Select all

1.2456=Mister Something
12.5=Mrs Someone Else
2.2456=Mister Something
2.2456=Mister Something
45.2=Uncle Who=45.2
Becuase I have to sort the results by highest number of hours.

I am terribly sorry for being dumb on this but please can you explain how I would get that into your solution?

(fwiw this is personal project not business and I will post the code here when it is done. And also add credit to anyone who has helped)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Tue May 17, 2022 6:36 pm

Ok I have got it in:

Code: Select all

ArrayCopy>ARRNICE,,Combined
And changed:

Code: Select all

Separate>value,=,item
Now I just need to get that back into an array instead of a continuous file so I can sort them.

Thank again guys.
Phil Pendlebury - Linktree

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Dorian (MJT support) » Tue May 17, 2022 6:51 pm

JRL wrote:
Tue May 17, 2022 3:44 pm
Here's my take:
Using arrays, no files. Take the list and place it into an array with Separate>. Sort the array using ArraySort> then write to a new list each time a new name arrives summing the numbers if the name repeats.

Started the repeat at 1 to skip over the blank line created by LabelToVar>

Code: Select all

/*
List:
Chris 5.400
Steve 6.230
Phil 0.501
Steve 2.001
Steve 3.000
Phil 1.00
*/

LabelToVar>List,vList
Separate>vList,crlf,combined

ArraySort>Combined

Let>MergedList=
Let>BB=1
Repeat>BB
  Add>BB,1
  Let>value=Combined_%BB%
  Separate>value, ,item
  If>BB=2
    Let>CurrName=item_1
    Let>CurrSum=0
  EndIf
  If>CurrName=item_1
    Let>CurrSum=CurrSum+item_2
  Else
    Let>MergedList=%MergedList%%CurrName% %CurrSum%%crlf%
    Let>CurrSum=item_2
  EndIf
    Let>CurrName=item_1
Until>BB={%combined_count%+1}

MDL>MergedList
Aaah, that's the job! :)
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Wed May 18, 2022 2:30 pm

Here you go guys, almost finished:

Image
Phil Pendlebury - Linktree

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Dorian (MJT support) » Wed May 18, 2022 3:04 pm

Aaah, well look at that! Thank you for the credit.

Is that a gaming timer?

It would need to be a very long digit for my Minecraft/FarmSim/Forza stats. :D
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
JRL
Automation Wizard
Posts: 3460
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Finding duplicates in an array and adding figures... Puzzle

Post by JRL » Wed May 18, 2022 9:54 pm

Cool!

Not a gamer. My Buddy Dave got the Atari Tank game back around 1977(?) We started playing Friday afternoon and played continuously until the wee hours of Sunday morning. I'd only been married for a short time then and was nearly divorced because of my "addiction". Have steered clear of video games since.

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Thu May 19, 2022 3:58 am

Thanks for reading guys, The app is basically a folder/file parser on steroids.

It is for Microsoft Flight Sim.

It reads the data from all aircraft folders and gives a report on the hours you have flown in that aircraft. Some aircraft have different models, hence the need to use a lookup file which gives all models of the same aircraft a certain specific name. This is why I needed the code in this thread.

End result looks like this:

Code: Select all

MSFS Hours Counter - Version 1.85b - by Phil Pendlebury
Discord: https://discord.gg/9y4RNtJDSv
hours:minutes (by total Hobbs, highest first) = aircraft name
hhhh:mm-------------------------------------------------------
--------------------------------------------------------------
0033:03 = Quest (Daher) Kodiak 100 [SimWorks Studios]
0025:32 = Cessna 152 [Asobo]
0021:56 = Piper PA-28-161 Warrior II [Just Flight]
0018:25 = DHC-6 Twin Otter [Aerosoft]
0016:11 = FG-1D Corsair [MilViz]
0014:31 = Piper PA-28R Arrow III/IV (NA and Turbo) [Just Flight]
0014:20 = Airbus A320 Neo [Asobo]
0013:24 = Cessna 172 [Asobo]
0008:12 = Douglas DC-6 [PMDG]
0006:53 = Concorde [DC Designs]
0006:40 = BN2 Islander [BlackBox Simulations]
0006:31 = Cessna C208B Grand Caravan EX [Asobo]
0006:03 = Spitfire LF Mk IXc [Flying Iron Simulations]
I now have 2 other issues connected to this so I may as well type here:

1. Compiled App is still getting flagged as Virus by some user's machines.
2. Graphics (as seen in previous screenshot), are the wrong size when user is using Windows Scaling. This includes the size of the memo field:

Image

If anyone has any thoughts on the above it may be useful for all users too. :)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Finding duplicates in an array and adding figures... Puzzle

Post by Phil Pendlebury » Fri May 20, 2022 6:42 pm

Ok I will post to other threads. Thanks again guys:

Virus Warning Issue: viewtopic.php?f=2&t=11153

Scaling Issue: viewtopic.php?f=2&t=11161
Phil Pendlebury - Linktree

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts