Solved

Regular Expression:  get third field and only single instances

Posted on 2004-04-22
2
270 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 84

Accepted Solution

by:
ozo earned 50 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now