?
Solved

Regular Expression:  get third field and only single instances

Posted on 2004-04-22
2
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

752 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