Re: Grep uniqueness issue



On Jul 29, jason_normandin@xxxxxxxxxxx said:

I am having an odd problem using grep to ensure an array only contains distinct entries.

You should probably use a hash (along with your array, if you need to keep the order they're in) to ensure unique-ness.


support01-FastEthernet1/0
support01-RH
jnormandin-p370-1691-SH-Cpu-2
jnormandin-p370-1691-SH

I am trying to create an ARRAY containing each of these entries only once (a distinct list). I am using a grep statement:

push @pingErrorsName, $element if (! grep (/\b$element\b/, @pingErrorsName));

There's NO reason to use a regex here at all. You don't want $element to be treated like a regex (since it's just a string). If you were gonna use grep(), you should just do


  push @pingErrorsName, $element if !grep $_ eq $element, @pingErrorsName;

But I suggest you use a hash:

  push @pingErrorsName, $element if !$uniq{$element}++;

Read 'perldoc -q uniq' for more information.

* Where $element contains one of the above names in the list and @pingErrorsName is the distinct list of elements.

What I am finding is that the array will contain all of the correct entries except: jnormandin-p370-1691-SH. It appears as though the grep is matching jnormandin-p370-1691-SH to the jnormandin-p370-1691-SH-Cpu-2 string (as it is a substring of the second one).

Well, YES. That's because \b matches word boundaries. Between the 'H' and the '-' is a word boundary.


--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart
.



Relevant Pages

  • Re: Range Object Misunderstanding
    ... Public gStrArray() As String ... Public Function rngSrtAs String, ... ' "List" is string array for sorting ... entries from the named range "Groups". ...
    (microsoft.public.excel.programming)
  • Re: string dictionary and memory issue.
    ... ince I am easily loading over 300 000 strings in memory, ... a flag representing the end of a string. ... How many entries are in each node? ... suggest using a plain array, either as a list of entries (for nodes ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finding the number of occurences in an array
    ... easy way to find out how many entries in an array match a string? ... the entries in @gfnames contain the string ".txt" without going through ... You use the magic wand to transform the array into a count! ...
    (comp.lang.perl.misc)
  • Re: CString Array to LPCTSTR *
    ... of string where he has a diagram with segment descriptors and each of the ... entries in the descriptor table points to segments of string. ... "For vector the element data structure is most likely an array" ...
    (microsoft.public.vc.mfc)
  • Re: converting bash-scripts into GUIs
    ... a window with various entries, check buttons, option buttons/menus, ... dialog is limited to shell windows (it is not a 'true' GUI in a sense). ... check buttons (for selecting (or not) optional values) ... string Y. The script takes four fixed parameters and two options: ...
    (comp.os.linux.misc)