+ Reply to Thread
Results 1 to 4 of 4

Is there a better way? (There always is.) - HOWTO: Replace repeated text in an array.

  1. #1
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Is there a better way? (There always is.) - HOWTO: Replace repeated text in an array.

    I'm working with Cisco router data. In a router dump, there will be lines that say "interface <some interface_name>".

    I load an array with this:
    Please Login or Register  to view this content.
    FWIW, 'FilterRegEx' is a function I wrote that does what it's name says (I'll include it below, for critique and improvement).

    The array I get will contain every line in my router data pull that starts with "interface Vlan*". But, I don't want every element to start with "interface ".
    Here is my one-line cleanup (is this the best way?):
    Please Login or Register  to view this content.
    The reason I'm asking is this the most efficient way to do this?

    As promised, here is my FilterRegEx function:
    Please Login or Register  to view this content.
    Last edited by Rhudi; 02-22-2023 at 03:42 PM. Reason: Typo

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Is there a better way? (There always is.) - HOWTO: Replace repeated text in an array.

    IMHO, you have many unneeded operations, e.g. :

    1. You write the matches to string first (not directly to an array) :
    Please Login or Register  to view this content.
    2. Then split the string to array :
    Please Login or Register  to view this content.
    3. Then :
    a. join the array again to become a string
    b. remove "interface" word
    c. split again the string into an array
    Please Login or Register  to view this content.
    So number of operations are :
    [1] String concatenation
    [2] Split
    [3] Join
    [4] Replace string
    [5] Split


    Here is my proposed code (still in the form as a sub, you can convert them to a function later) :
    Please Login or Register  to view this content.
    Only one operation needed, that is assigning the matches to an array from these lines :
    Please Login or Register  to view this content.
    The pattern itself is :
    Pattern = "^interface (Vlan[^\r\n]+)"
    the word "interface" is put outside brackets, only Vlan.... that put inside brackets, so this became a submatch, which we can retrieve from v2.SubMatches(0)
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Re: Is there a better way? (There always is.) - HOWTO: Replace repeated text in an array.

    Thank you for your feedback!

    I am learning proper RegEx patterns, so this redesign of the RegEx Filter is promising. I will put my 'FiterRegEx' function side by side with your test sub and mash the best bits together.

    I use my FilterRegEx function for many other reasons. My source data is a router config dump that could contain up to and or over 80k lines of text.

    I could start an entire sub-thread with all the functions I've written. I know many of them are horrific kludgeter flocks.

    My source text file is always read into a Public array that will be filtered by many different subs. I suppose it's conceivable that instead of loading an array, there may be some way to read the text file directly for my filtering. Tho, loading the file into an array gives me RAM speed vs any file IO.

    My go-to method for testing RegEx searches against my data is using Notepad++. I can't imagine any better text editor out there.

    Ultimately, your understanding of RegEx patterns exceeds mine.

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Is there a better way? (There always is.) - HOWTO: Replace repeated text in an array.

    You are welcome.

    Quote Originally Posted by Rhudi View Post
    Tho, loading the file into an array gives me RAM speed vs any file IO.
    I don't know how you read the file into an array, but there is a way to load entire file to a string, all at once with single statement (not incremental reading like using LINE INPUT method), so it is very fast, like this :
    Please Login or Register  to view this content.
    Then, if the content of the file is already in the string (in memory), then any process involving this string (e.g. our split string regex function to array) should be very fast.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Replace a Text in 255 character Array Formula
    By RXcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-12-2019, 01:47 AM
  2. RE: Can't Get BoSox Lineup to Automatically Time-Stamp and ReSort When New Batter Added
    By justinbelkin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2013, 05:35 PM
  3. [SOLVED] Append data to repeated values with a fixed array to be repeated on value change
    By anchuri_chaitanya in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-06-2011, 12:30 AM
  4. Howto concatenate Array values
    By rwgrietveld in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-22-2008, 01:28 PM
  5. [SOLVED] HowTo:Validation Formula = Array of strings
    By AMDRIT in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2006, 04:55 PM
  6. [SOLVED] Printing text in a repeated cell/row that is longer than repeated
    By Valerie Dyet in forum Excel General
    Replies: 1
    Last Post: 02-12-2006, 11:30 PM
  7. [SOLVED] HowTo add a period to front of EVERY text cell in a Excel column
    By adamcollegeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-24-2005, 03:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1