On Wed, Aug 22, 2012 at 4:39 PM, jet speed <speedj...@googlemail.com> wrote:
> Hi All, > > Please advice me on now to capture the data below in the format as below. > > i thought of using hash, but then the problem is each DisplayDevNum has > multiple WWN. some has 4 elements, some has 2. Any other method ? > Apprecaite your comments. > > > i want to caputre in the below format. > DisplayDevNum and its corresponding WWN=10:00:00:00:c9:c9:xx:xx and > nickname= xxxxx > DisplayDevNum and its corresponding WWN=10:00:00:00:c9:c9:xx:xx and > nickname= xxxxx > > ---- > > DisplayDevNum=1D:D4 > domainID=7 > devNum=8,888 > List of 4 WWN elements > WWN=10:00:00:00:c9:c9:89:8c > nickname=res-abc > WWN=10:00:00:00:c9:c9:89:8b > nickname=res-abd > WWN=10:00:00:00:c9:c9:89:8a > nickname=res-a33 > WWN=10:00:00:00:c9:c9:89:8i > nickname=res-34 > DisplayDevNum=1D:D9 > domainID=7 > devNum=8,888 > List of 2 WWN elements > WWN=10:00:00:00:c8:c9:89:8f > nickname=res-a1 > WWN=10:00:00:00:c9:c9:89:81 > nickname=res-a33 > > Sj > Hi Sj, The hash idea is a good one but it is going to be a little bit more complex then a simple hash. You will first need to record the DisplayDevNum (and remember it, then find all the underlying information and associate it with the DisplayDevNum. The following example does just that: use strict; use warnings; use Switch; use Data::Dumper; my $DisplayDevNum_tracker; my $WWN_tracker; my %complex_hash; open my $fh, "<", "text.txt" or die $!; while ( <$fh> ) { chomp; my $line = $_; my ( $key, $value ) = split/=/,$line; switch ( $key ) { case 'DisplayDevNum' { $DisplayDevNum_tracker = $value; $complex_hash{ $value } = {}; } case 'domainID' { $complex_hash{ $DisplayDevNum_tracker }{'domainID'} = $value; } case 'devNum' { $complex_hash{ $DisplayDevNum_tracker }{'devNum'} = $value; } case 'WWN' { $WWN_tracker = $value; $complex_hash{ $DisplayDevNum_tracker }{ $value } = {}; } case 'nickname' { $complex_hash{ $DisplayDevNum_tracker }{ $WWN_tracker } = $value; } } } print Dumper %complex_hash; In short open the file loop over it one line at a time, remove the linefeed. Then split on the = sign automatically dropping lines you do not care about. Then using a switch statement figure out what type of line you are dealing with. If it is a DisplayDevNum line remember the value. if the key is anything else use the remembered DisplayDevNum to associate the new data with it. If the WWN line is found remember the value and once you get to the nickname associate this with the remembered WWN value. Printing it all out you get a hash that looks like this: $VAR1 = '1D:D9'; $VAR2 = { 'devNum' => '8,888', 'domainID' => '7', '10:00:00:00:c9:c9:89:81' => 'res-a33', '10:00:00:00:c8:c9:89:8f' => 'res-a1' }; $VAR3 = '1D:D4'; $VAR4 = { '10:00:00:00:c9:c9:89:8a' => 'res-a33', 'devNum' => '8,888', 'domainID' => '7', '10:00:00:00:c9:c9:89:8b' => 'res-abd', '10:00:00:00:c9:c9:89:8c' => 'res-abc', '10:00:00:00:c9:c9:89:8i' => 'res-34' }; Which is I believe what you are looking for right? Regards, Rob