Fastest alternatives to flattening a non-uniform nested array with regex?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fastest alternatives to flattening a non-uniform nested array with regex?
# 1  
Old 03-30-2018
Fastest alternatives to flattening a non-uniform nested array with regex?

Hello,

I'm looking at simplfying a function that flattens an array, it uses recursion and filters objects by type but this seems to be a waste of resources to me at least, using conditionals like this seems like a bad idea.

The array can be a generic type, int, string, float but not some container with mixed types.

For example
Code:
myArray:[Int]=[[-2, 5], 16, [-6, [9, [-3, [3, 1, 0]]]]]

Code:
for element in myArray 
{
  flatArray.append(element as! Int)
} else if element is [Any] { 
  recursionResult = flattenArray(nestedArray: element as! [Any])
  for num in recursionResult {
    flattenArray.append(num)
   }
}

The final result for this would be
Code:
[-2,5,16,-6,9,-3,3,1,0]

So I came up with this simple regex

Code:
/\-?[0-9]/g

Is there a more efficient way?
Thanks.
# 2  
Old 03-30-2018
My opinion:

As a general solution that seems fine. I have not validated it. Are you encountering performance problems?
BTW the PCRE regex is very well optimized and is regex-directed, so you can switch to that on most linux boxes with no problems.

Not that the 'old' regex code is not optimized.

I like your solution, but I wonder about what you are seeing as a problem. Selecting a regex engine is a lot messier than in long ago times:
Comparison of regular expression engines - Wikipedia

However PCRE is considered to be as good as it gets. You could write a simple single purpose C program....

Last edited by jim mcnamara; 03-30-2018 at 10:58 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-31-2018
Quote:
Originally Posted by jim mcnamara
My opinion:

As a general solution that seems fine. I have not validated it. Are you encountering performance problems?

You could write a simple single purpose C program....
Thanks for the comments.

Yes, I tried some long, really off-the-wall, random arrays that would be equivalent to a messy json structure and the simple regex easily beat the recursion.

Good idea about C code, what do you think about the performance gain and memory utilization of using a threaded version? versus openmp/mpi? or versus GPU, if the incoming array is a continuous stream from a web socket? or similar.

For single precision, Int16 or UInt16, it seems like GPU would crush this problem but I'm unsure of the overhead required for the memory allocation.

Last edited by f77hack; 03-31-2018 at 06:52 PM.. Reason: add memory stuff
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uniform Spacing in the message

Hello, I am running a script which sends an output as an email; I am having issues with the spacing being not uniform in the message. Snippet of the code and email message below: if ] then echo "$Hostname\tMISSING\tHMCBackup" >> $BackupMsg else if ] then echo... (12 Replies)
Discussion started by: hasn318
12 Replies

2. Shell Programming and Scripting

How to insert an array element within regex?

Hello to all, I'm trying to separate the string "str" using a regex within match function. The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL. For this string... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. AIX

Uniform LUN size

Hi, Is there any advantage is making all my storage LUNS ( hdisk ) of uniform size. Currently the LUN's are having different size () eg: 50G / 60G / 75G etc ). I am planning for a storage migration....so should i go for uniform lun size or with current LUN size pattern ? Regards, jibu (3 Replies)
Discussion started by: jibujacob
3 Replies

5. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

6. Shell Programming and Scripting

Grep: Searching with a regex that contains a variable from an array

I'm attempting to grep for lines formatted like this: grep -e '^\\",' Any suggestions as to why this isn't working? ---------- Post updated at 05:03 PM ---------- Previous update was at 04:17 PM ---------- This was my solution: grep -e '^\'\",' It's hard to read, but basically I... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

7. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies
Login or Register to Ask a Question