Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The code works, just want to make sure I am doing it the best way. 1

Status
Not open for further replies.

coffeysm

MIS
Oct 7, 2004
571
0
0
US
Hi guys, I am not a Perl expert, so I was just hoping someone could maybe preview this and let me know if there is anything I could do better. Below is an example of the output I am trying to parse, I am mostly concerned with the data under Name: for example "PHRCFL-xx-xxx".

Code:
Compellent Command Utility (CompCU) 5.2.1.2
=================================================================================================
User Name:              Admin
Host/IP Address:        10.10.1.10
Single Command:         volume show
=================================================================================================
Connecting to Storage Center: 10.10.1.10 with user: Admin
Running Command: volume show 

Index  Name                             Status   ConfigSize      ActiveSize      ReplaySize      Folder                                                           StorageProfile                   DeviceID                         SerialNumber      ConfigSizeBlock ActiveSizeBlock ReplaySizeBlock MaxWriteSizeBlo ReadCache  WriteCache 
------ -------------------------------- -------- --------------- --------------- --------------- ---------------------------------------------------------------- -------------------------------- -------------------------------- ----------------- --------------- --------------- --------------- --------------- ---------- ---------- 
14     PHRCFL-xx-xxx                    Up       300.00 GB       218.66 GB       0.00 KB         Examiners/wbrooks                                                RAID 10 and RAID 5 Tier 1        6000d3100007fe00000000000000000f 000007fe-0000000f 629145600       458567680       0               0               Disabled   Enabled

This is a listing off all my SAN volumes and I want to put just that field into an array. I wrote up the following and it seems to work, but I was just hoping someone could see if there is anything I could improve upon.

Code:
my @data = `command above`

# I added this to remove all the top information login, address, etc..

splice(@data,0,12);
# Remove the carriage returns.
chomp(@data);

# delete the last three array members, they contain success messages.

for ($i = 0; $i < 3; $i++){
 pop(@data);
}

foreach my $var (@data){

#I wanted to use white spaces, but some of my users put spaces in their volume names, so I went with Up since this should not change.

@index = split(/Up/, $var);

# I did not want all that other information just sitting there

delete $index[1];

# I set another variable to delete the first seven characters and it leaves me with just the volume name, which is what I want.

my $vol = $index[0];
$vol = substr($vol,7);

# Put the name into a new array I made.

push(@volumes, $vol);
}

# Sort the list alphabetically

@volumes = sort {$a cmp $b } @volumes;

# Print the volumes or put them in a listbox

foreach (@volumes){
 print $_;
 $drpvolume->insert('end',$_);
}
 
When you say "I added this to remove all the top information login, address, etc.."... are those 12 lines not shown in the example you gave here? If they are, it seems like you are splice-ing too many lines?

I would trigger my parsing based on the line of dashes under the column headings, and terminate upon reaching the first success message (or blank line, if there is one?), along these lines:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@data[/blue] = [red]`[/red][purple]command above[/purple][red]`[/red]

[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]@data[/blue][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]@volumes[/blue][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue] = [fuchsia]0[/fuchsia][red];[/red] [blue]$i[/blue] < [blue]@data[/blue][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
        [gray][i]# when line before volume listing is encountered[/i][/gray]
        [olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue][red][[/red][blue]$i[/blue][red]][/red] =~ [red]/[/red][purple]^------ [/purple][red]/[/red][red])[/red] [red]{[/red]
                [gray][i]# push all volume names until first success message[/i][/gray]
                [olive][b]for[/b][/olive] [red]([/red][blue]$i[/blue]++[red];[/red] [blue]$i[/blue] < [blue]@data[/blue] && [blue]$data[/blue][red][[/red][blue]$i[/blue][red]][/red] !~ [red]/[/red][purple]success1[/purple][red]/[/red][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
                        [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@volumes[/blue],[url=http://perldoc.perl.org/functions/substr.html][black][b]substr[/b][/black][/url][red]([/red][blue]$data[/blue][red][[/red][blue]$i[/blue][red]][/red],[fuchsia]7[/fuchsia],[fuchsia]32[/fuchsia][red])[/red][red];[/red]
                [red]}[/red]
        [red]}[/red]
[red]}[/red]

[gray][i]# default sort should suffice I think[/i][/gray]

[blue]@volumes[/blue] = [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [blue]@volumes[/blue][red];[/red]

[gray][i]# Print the volumes or put them in a listbox[/i][/gray]

[olive][b]foreach[/b][/olive] [red]([/red][blue]@volumes[/blue][red])[/red][red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]                   [gray][i]# new line required[/i][/gray]
        [gray][i]#$drpvolume->insert('end',$_);[/i][/gray]
[red]}[/red]

Annihilannic.
 
Code:
Compellent Command Utility (CompCU) 5.2.1.2
=================================================================================================
User Name: Admin
Host/IP Address: 10.10.1.10
Single Command: volume show
=================================================================================================
Connecting to Storage Center: 10.10.1.10 with user: Admin
Running Command: volume show

Index Name Status ConfigSize ActiveSize ReplaySize Folder StorageProfile DeviceID SerialNumber ConfigSizeBlock ActiveSizeBlock ReplaySizeBlock MaxWriteSizeBlo ReadCache WriteCache
------ -------------------------------- -------- --------------- --------------- --------------- ---------------------------------------------------------------- -------------------------------- -------------------------------- ----------------- --------------- --------------- --------------- --------------- ---------- ----------

These are the 12 lines I was referring to when, I started doing the splicing. I didn't even think of using the dashes as a marker thanks for pointing that out. There are one or two blank lines before the first success message appears. Yours looks a lot more simpler than what I worked up, I will test it tomorrow. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top