?
Solved

Regular Expression:  get third field and only single instances

Posted on 2004-04-22
2
Medium Priority
?
316 Views
Last Modified: 2012-05-04
I need a   Regular Expression to add to the following

$mediaServerName = ($storageUnit =~

that will pull put the third line and place only single instances of the same field into the array. The $storageUnit  looks like:


STK9310_L00_DB00D02 2 db00d02 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 4 0 "*NULL*" 0 2 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  

and I would like for the array to have db00d02 and db00m01.


NOT: db00d02 ,db00m01 and db00m01
0
Comment
Question by:MatthewF
2 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10887750
Hi MatthewF,
you can use a hash to ensure there is only a single instance

%hsh;
while(<>) {
  @line =split;
  if ( not $hsh{$line[0]}) {
    $hsh{$line[0]} = $line[2];
}

now you can traverse the hash and do what you like with it (store it in an array, print it etc.)

hth
leflon
0
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 10889554
$storageUnit = <<ENDHERE;
STK9310_L00_DB00D02 2 db00d02 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 4 0 "*NULL*" 0 2 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*
ENDHERE
my %seen=(''=>1);
@array = grep{!$seen{$_}++}map{(split)[2]}split/\n/, $storageUnit ;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question