For each line in the Variable do?

Hi I get the following result in my variable and what I want to do is for each of the result I want to execute the same macro actions for each do …

KM should recognise each as;
10680042
12088057
12324057
etc

How is this possible? also for that particular number that its working on that moment I would like to save that number as a KM Variable so I can use in other places too.
Also I need to remove duplicate results this is very important.

{{}, {}, {}, {}, {{10680042}}, {{10680042}}, {{12088057}}, {{12323057}}, {{12324057}}, {{12325057}}, {}, {{13085070}}, {}, {{13850070}}, {{14330070}}, {{14331070}}, {{14801070}}, {{15175070}}, {{15948070}}, {{15949070}}, {{15950070}}, {{15951070}}, {{16548002}}, {{17654002}}, {{17686002}}, {{18109010}}, {{18110010}}, {{18111010}}, {{18112010}}, {{18122010}}, {{18232010}}, {{18233010}}, {{18234010}}, {{18235010}}, {{18370010}}, {{18371010}}, {{18372010}}, {{18759010}}, {{18813010}}, {{18814010}}, {{18815010}}, {{18816010}}, {{18935010}}, {{18983010}}, {{18989010}}, {{19025010}}, {{19027010}}, {{19028010}}, {{19145010}}, {{19238010}}, {{19264010}}, {{19277010}}, {{19286010}}, {{19309010}}, {{19323010}}, {{19324010}}, {{19325010}}, {{19365010}}}}}, {{12088057}}, {{12323057}}, {11444057{12324057}}, {{12325057}}, {}, {{13085070}}, {}, {{13850070}}, {{14330070}}, {{14331070}}, {{14801070}}, {{15175070}}, {{15948070}}, {{15949070}}, {{15950070}}, {{15951070}}, {{16548002}}, {{17654002}}, {{17686002}}, {{18109010}}, {{18110010}}, {{18111010}}, {{18112010}}, {{18122010}}, {{18232010}}, {{18233010}}, {{18234010}}, {{18235010}}, {{18370010}}, {{18371010}}, {{18372010}}, {{18759010}}, {{18813010}}, {{18814010}}, {{18815010}}, {{18816010}}, {{18935010}}, {{18983010}}, {{18989010}}, {{19025010}}, {{19027010}}, {{19028010}}, {{19145010}}, {{19238010}}, {{19264010}}, {{19277010}}, {{19286010}}, {{19309010}}, {{19323010}}, {{19324010}}, {{19325010}}, {{19365010}}}

My aim after removing the duplicate, save each result in turn to a variable then execute a particular set of macro actions… :slight_smile:

Many thanks for your help

Hey Ali,

Is the mess in the curly brackets the data you’re working with?

If so — what is its source?

If it’s an AppleScript then something is fishy, because it won’t compile in Script Debugger.

-Chris

no it’s from Daylite I am pulling out data in an array, yes I am pulling out from AppleScript.

To get a single, clean list, like the above, from data like this:
{{}, {}, {}, {}, {{10680042}}, {{10680042}}, {{12088057}}, {{12323057}}, {{12324057}}, {{12325057}}, {}, {{13085070}}, {}, {{13850070}}, {{14330070}}, {{14331070}}, {{14801070}}, {{15175070}}, {{15948070}},

You can just use 2 Search & Replace Actions on the Source Data:

  1. Search for any of these characters, and replace with NOTHING:
    (the bracket [] tell the RegEx engine to use ANY of the characters between them)
    Search using RegEx: [{} \n]
    Replace: <nothing> (leave the replace field blank)
  • Search for commas, and replace with linefeed:
    Search: ,
    Replace: \n

You should already know how to do this. We answered that question for you here:
Removing duplicate strings from Array stored as KM Variable[quote="demirtas1, post:1, topic:7059"]
My aim after removing the duplicate, save each result in turn to a variable then execute a particular set of macro actions.
[/quote]

I hope you don't mind me saying so, but I think that is a bad idea.
We could create the KM Variables, but having tens of Variables named Var01, Var02, ... Var50 would be a nightmare to use later.

Instead, you need some way to identify what each number extracted is used for. Account numbers?

One approach is to build a single KM Variable which contains all of the numbers, one per line, and use them in a KM For_Each action (KM Wiki) on a sequential basis, OR extract a particular number based on line# or part of the number, using a Search_Variable action (KM Wiki) with a RegEx.

###Need More Info
Before we spend any more time trying to come up with specific solutions, please post in detail the workflow you want to automate.

1 Like

Hi,

Thank you for your response.

For each line the result should be
10680042
12088057
12324057
in each line, KM then grab 1 of these at a time (10680042) and save it under the same Variable (Var01) and then execute the same macro actions. Then grab the second line (12088057) and save it under the same Variable name (Var01) as the first one replacing whats already there and then execute the same macro actions. repeat for the next line. We don’t want to name them as Var01, Var 02 etc…

Perfect many thanks for your help I have resolved it

Hey Ali,

You're not being clear.

Is that data text?

Is that data an AppleScript list object?

In either case it's very easy to deal with using AppleScript and the Satimage.osax.

TEXT:

set dataStr to "{{}, {}, {}, {}, {{10680042}}, {{10680042}}, ..."

set foundText to find text "\\d+" in dataStr with regexp, all occurrences and string result
set foundText to sortlist foundText with remove duplicates

 
APPLESCRIPT-LIST-OBJECT

set dataList to {{}, {}, {}, {}, {{10680042}}, {{10680042}}}

set AppleScript's text item delimiters to linefeed
set dataList to dataList as text
set foundText to find text "\\d+" in dataList with regexp, all occurrences and string result
set foundText to sortlist foundText with remove duplicates

-Chris

Hey Ali,

Here’s another method that requires only built-in macOS components.

This method will work with either AppleScript-List-Object or plain-text input.

set numList to {{}, {}, {}, {}, {{10680042}}, {{10680042}}, {{12088057}}, {{12323057}}, {{12324057}}, {{12325057}}, {}, {{13085070}}, {}, {{13850070}}, {{14330070}}, {{14331070}}, {{14801070}}, {{15175070}}, {{15948070}}, {{15949070}}, {{15950070}}, {{15951070}}, {{16548002}}, {{17654002}}, {{17686002}}, {{18109010}}, {{18110010}}, {{18111010}}}

# Collapse the nested list(s) to text.
set AppleScript's text item delimiters to linefeed
set numList to numList as text

# Extract the numbers to a list and then coerce back to text.
set numList to (words of numList) as text

# Use the shell to sort and return only unique items.
set numList to do shell script "sort -u <<< " & quoted form of numList

-Chris