RE: Grep uniqueness issue



jason_normandin@xxxxxxxxxxx <mailto:jason_normandin@xxxxxxxxxxx> wrote:
> Hey Guys
>
> I am having an odd problem using grep to ensure an array only
> contains distinct entries.
>
> I have a list similar to the following in a file (short example of a
> much longer list)
>
> support01-FastEthernet1/0
> support01-RH
> jnormandin-p370-1691-SH-Cpu-2
> jnormandin-p370-1691-SH
>
> These entries may or may not appear multiple times within that list.
>
> 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));

push @pingErrorsName, $element if ( ! grep { $_ eq $element }
@pingErrorsName );

or

push @pingErrorsName, $element if ( ! grep { /^$element$/ }
@pingErrorsName );

should work.. (untested though).

But this would be inefficient for large arrays.

Use hash instead.

$pingErrorsName{$element} = undef;

Retrieve the elements by

@elements = (keys %pingErrorsName);

> * 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).
>
> Now I am using word boudnary anchors (\b) in the grep so I am
> confused as to why this is not working.
>
> Does anyone have any ideas as to why this is occuring and how I can
> prevent it?

HTH...

--Ankur

Why did kamikaze pilots wear helmets anyways?
.



Relevant Pages

  • Re: Grep uniqueness issue
    ... > I am having an odd problem using grep to ensure an array only contains ... > These entries may or may not appear multiple times within that list. ... > I am trying to create an ARRAY containing each of these entries only once ... I am using a grep statement: ...
    (perl.beginners)
  • Grep uniqueness issue
    ... I am having an odd problem using grep to ensure an array only contains distinct entries. ... I am trying to create an ARRAY containing each of these entries only once. ...
    (perl.beginners)
  • Re: Re: Grep uniqueness issue
    ... I need to avoid using any OS dependant commands. ... >> I am having an odd problem using grep to ensure an array only contains ... >> These entries may or may not appear multiple times within that list. ... >> I am trying to create an ARRAY containing each of these entries only once ...
    (perl.beginners)
  • Re: Find a line in a text file then print a field
    ... Here is "grep" part of my perl script so far (ommitting the various ... the matching lines in an array. ...
    (perl.beginners)
  • Re: How to sort an array which contains a value several times ?
    ... Jay Savage a e'crit: ... > because we want to iterate over @array and filter duplicates out (you ... > cases $is_new remains 0 and grep filters that element out. ...
    (perl.beginners)