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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question on Arrays

Status
Not open for further replies.

TheDugsBaws

Technical User
Oct 23, 2007
114
EU
I have a script at work that checks to see if a VM snapshot exists and will print out the results. I can 'read' these scripts but not write them.

Could someone help with the following?

my $snapshots = $_->snapshot;

It searchs this against a vmname and will output the name of the snapshots found.

How can I amend this to output the NUMBER of snapshots found?
 
You need to explain more about the module you use or consult the documentation for it.
If you get the name (names?) of the snapshots found, you should be able to count them, though this is not necessarily the most efficient method.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Ok, here is my subroutine
sub list_snapshot {
my $vm_views = VMUtils::get_vms ('VirtualMachine', $vm_name, $datacenter, $folder,
$pool, $host, %filter_hash);
foreach (@$vm_views) {
my $mor_host = $_->runtime->host;
my $hostname = Vim::get_view(mo_ref => $mor_host)->name;

my $count = 0;
my $snapshots = $_->snapshot;

if(defined $snapshots) {
Util::trace(0,"\nSnapshots for Virtual Machine ".$_->name
. " under host $hostname\n");
printf "\n%-47s%-16s %s %s\n", "Name", "Date","State", "Quiesced";
print_tree ($_->snapshot->currentSnapshot, " "
, $_->snapshot->rootSnapshotList);

}
else {
Util::trace(0,"\nNo Snapshot of Virtual Machine ".$_->name
." exists under host $hostname\n");
}
}
}

It will print the names of the VM snapshots, but I want it to print how many snapshots there are.
 
Ok I've actually changed this around to simplify it for myself.

my @vmname = ($numclones)


sub read {

$basevm = Opts::get_option("basevm");
#basevm =test

my $count = 1;

while ($count <= $numclones)
#numclones = 5
{
@vmname = $basevm."_clone_000".$count;
$count++;
printf @vmname;

}

}

Can anyone tell me why:

1 - If I print the array @vmnae inside the loop I get

test_clone_001test_clone_002test_clone_003test_clone_004test_clone_005

But if I print it outside the array I get test_clone_005

Ideally I want to create a array of size 5 and have that available outisde that loop

2 - How do I pass this to another array?

Is it: push (@another,@vmname)
 
As you are in a loop and reassignning a value to @vmname, you will only be left with the last assignment value outside the loop.

If you want to add items toi an array, yes use push...

Code:
push (@vmname, $basevm."_clone_000".$count);

then outside the loop you will @vmname array with $count number of members.

Side note, you don't create arrays with dimensions in perl, arrays in perl are dynamic in size like 'stacks' in other languages, but the beauty is you can access any member of the array with an index
Code:
print $vmname[3] # prints the 4 item in the array

oh an remember indexing in perl start at Zero!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top