jet speed wrote:
> Hi All,
> 
> I put togather a piece of code with some help, to capture the output as
> below from a file "cxout1" as below. I am sure this can be written in few
> lines or even differnt way of getting the same out put with use of hash etc.
> Kindly help me to optimize the code. Any help would be much appericated.
> 
> output from the script
> ---------------------------------
> 
> The Clarion Array Serial Number is CK200061101100
> 
> LUN INFO         POLICY-TYPE             OWNER           PSEUDO DEVICE
> LUN 415          policy=CLAROpt          current=SP A            emcpower3a
> LUN 815          policy=CLAROpt          current=SP B            emcpower4a
> 
> file contents cxout1
> 
> -----------------------------
> 
> Pseudo name=emcpower3a
> CLARiiON ID=CK200061101100 [JAZZ]
> Logical device ID=600601601496160012288D48703EDB11 [LUN 415]
> state=alive; policy=CLAROpt; priority=0; queued-IOs=0
> Owner: default=SP A, current=SP A
> ==============================================================================
> ---------------- Host ---------------   - Stor -   -- I/O Path -  -- Stats
> ---
> ### HW Path                 I/O Paths    Interf.   Mode    State  Q-IOs
> Errors
> ==============================================================================
> 2310 [EMAIL PROTECTED]/[EMAIL PROTECTED]/[EMAIL PROTECTED]       c3t20d0s0 SP 
> A0     active  alive      0
> 1
> 2310 [EMAIL PROTECTED]/[EMAIL PROTECTED]/[EMAIL PROTECTED]       c3t21d0s0 SP 
> A1     active  alive      0
> 1
> 
> Pseudo name=emcpower4a
> CLARiiON ID=CK200061101100 [JAZZ]
> Logical device ID=6006016014961600625987643E38DB11 [LUN 815]
> state=alive; policy=CLAROpt; priority=0; queued-IOs=0
> Owner: default=SP B, current=SP B
> ==============================================================================
> ---------------- Host ---------------   - Stor -   -- I/O Path -  -- Stats
> ---
> ### HW Path                 I/O Paths    Interf.   Mode    State  Q-IOs
> Errors
> ==============================================================================
> 2310 [EMAIL PROTECTED]/[EMAIL PROTECTED]/[EMAIL PROTECTED]       c3t20d1s0 SP 
> A0     active  alive      0
> 1
> 2310 [EMAIL PROTECTED]/[EMAIL PROTECTED]/[EMAIL PROTECTED]       c3t21d1s0 SP 
> A1     active  alive      0
> 1
> 
> 
> 
> my code
> 
> #!/usr/bin/perl -w
> 
> use strict;
> use warnings;
> use List::MoreUtils qw(:all);
> no warnings qw /syntax/;
> 
> 
> 
> #PRINT SERIAL NUMBER
> 
> my @sl;
> my $filename;
> $filename = "cxout1" ;
> open (FILE, "<$filename") or die "Could not open $filename: $!";
> while (<FILE>) {
> next unless $_ =~ /CK/;
> $_ = ~ (/\b(ID)\=(\w+)\s+/i)  ;
> push(@sl,$2);
> }
> 
> 
> # PRINT UNIQUE SERIAL NUMBER
> 
> my %seen = ();
> my @uniq = ();
> my $item;
> foreach $item (@sl) {
>     unless ($seen{$item}) {
>         # if we get here, we have not seen it before
>         $seen{$item} = 1;
>         push(@uniq, $item);
>     }
> }
> 
> 
> print "\nThe Clarion Array Serial Number is @uniq\n\n";
> 
> 
> # PRINT LUN INFORMATION
> 
> my @luns;
> #my $filename;
> $filename = "cxout1" ;
> open (FILE, "< $filename" ) or die "Could not open $filename: $!";
> while (<FILE>) {
>  chomp;
> # next unless $_ =~ /LUN/;
> # $_ =~ /\[(LUN\s+\d+)\]/;
> next unless /\[(LUN\s+\d+)\]/;
>  push(@luns,$1);
> 
> }
> 
> # PRINT POLICY INFORMATION
> 
> my @pol;
> #my $filename;
> $filename = "cxout1" ;
> open (FILE, "< $filename" ) or die "Could not open $filename: $!";
> while (<FILE>) {
>  chomp;
>  next unless $_ =~ /policy/;
>  $_ =  unpack ("x13 A14", $_);
>  push(@pol,$_);
> }
> 
> # PRINT OWNER INFORMATION
> 
> my @own;
> #my $filename;
> $filename = "cxout1" ;
> open (FILE, "< $filename" ) or die "Could not open $filename: $!";
> while (<FILE>) {
>  chomp;
> next unless $_ =~ (/\b(current)\=\w+/);
> $_ = unpack ("x21 A14",$_);
> push(@own, $_);
> }
> 
> #PRINT PSEUDO INFORMATION
> 
> my @pseudo;
> open (FILE, "< $filename" ) or die "Could not open $filename: $!";
> while (<FILE>) {
>  chomp;
> next unless $_ =~ (/\b(Pseudo)/);
> $_ = unpack ("x12 A18", $_);
> push (@pseudo, $_);
> }
> 
> #CONSOLIDATE & PRINT
> 
> my $result = each_array(@luns, @pol, @own, @pseudo);
> 
> print "LUN INFO\t POLICY-TYPE\t\t OWNER\t\t PSEUDO DEVICE\t\t\n";
> 
> 
> while ( my ($a, $b, $c, $d) = $result->() )
> {
> 
>     print "$a\t\t $b\t\t $c\t\t $d\t\t\n";
> }

The main way to simplify your code is to extract all the data in a simgle pass
of the file. Even if you want to make several passes it is better to write

  seek FILE, 0, 0;

to start reading from the beginning again instead of reopening the file for each
pass.

My first attempt at your program gave me this.

HTH,

Rob



use strict;
use warnings;

use List::MoreUtils qw(:all);

#PRINT SERIAL NUMBER

my $filename = 'cxout1' ;

my %sl;
my @luns;
my @pol;
my @own;
my @pseudo;

open my $fh, '<', $filename or die "Could not open $filename: $!";

while (<$fh>) {
  $sl{$1}++ if /\bID=(CK\w+)/i;
  push @luns, $1 if /\[(LUN\s+\d+)\]/;
  push @pol, $1 if /\b(policy=\w+);/;
  push @own, $1 if /\b(current=[\w+ ]+)/;
  push @pseudo, $1 if /\bPseudo name=(\w+)/;
}

# PRINT UNIQUE SERIAL NUMBER
#
my @uniq = keys %sl;
print "\nThe Clarion Array Serial Number is @uniq\n\n";


#CONSOLIDATE & PRINT
#
my $result = each_array(@luns, @pol, @own, @pseudo);

print "LUN INFO\t POLICY-TYPE\t\t OWNER\t\t PSEUDO DEVICE\t\t\n";

while ( my @row = $result->() ) {
  printf "%s\t\t %s\t\t %s\t\t %s\t\t\n", @row;
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to