Hi jet speed,
  I think is a lot better using hash or hash reference as implemented
in perldsc to do this.
  Like so:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $display_dev_num = {};
my $id_name;

while ( defined( my $line = <DATA> ) ) {
    chomp $line;
    if ( $line =~ m{\ADisplay.+?=(.+?)$} ) {
        $id_name = $1;
        undef $display_dev_num->{$id_name};
    }
}
continue {
    my ( $name_tag, $tag_value ) = split /=/, $line;
    push @{ $display_dev_num->{$id_name}{$name_tag} }, $tag_value;
}

print Dumper $display_dev_num;

__DATA__
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
__END__

Your Dumper output is like so:

$VAR1 = {
          '1D:D9' => {
                       'DisplayDevNum' => [
                                            '1D:D9'
                                          ],
                       'devNum' => [
                                     '8,888'
                                   ],
                       'nickname' => [
                                       'res-a1',
                                       'res-a33'
                                     ],
                       'domainID' => [
                                       '7'
                                     ],
                       'List of 2 WWN elements' => [
                                                     undef
                                                   ],
                       'WWN' => [
                                  '10:00:00:00:c8:c9:89:8f',
                                  '10:00:00:00:c9:c9:89:81'
                                ]
                     },
          '1D:D4' => {
                       'List of 4 WWN elements' => [
                                                     undef
                                                   ],
                       'DisplayDevNum' => [
                                            '1D:D4'
                                          ],
                       'devNum' => [
                                     '8,888'
                                   ],
                       'nickname' => [
                                       'res-abc',
                                       'res-abd',
                                       'res-a33',
                                       'res-34'
                                     ],
                       'domainID' => [
                                       '7'
                                     ],
                       'WWN' => [
                                  '10:00:00:00:c9:c9:89:8c',
                                  '10:00:00:00:c9:c9:89:8b',
                                  '10:00:00:00:c9:c9:89:8a',
                                  '10:00:00:00:c9:c9:89:8i'
                                ]
                     }
        };

So, you can then pick out what you want. Is that simple with less work.

Hope this helps


On 8/22/12, jet speed <speedj...@googlemail.com> wrote:
> Hi Rob,
> Thanks for the detailed explanation. Appreciate it.
>
> Yes, perfect. All the details are captured.  i should be able to modify the
> output from your code.
>
> Thanks
> Sj
>
>
>
> On Wed, Aug 22, 2012 at 5:01 PM, Rob Coops <rco...@gmail.com> wrote:
>
>>
>>
>> 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
>>
>


-- 
Tim

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to