×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Newbie Question on Arrays

Newbie Question on Arrays

Newbie Question on Arrays

(OP)
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?
 

RE: Newbie Question on Arrays

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
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Newbie Question on Arrays

(OP)
Ok, here is  my subroutine

Quote:


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.

RE: Newbie Question on Arrays

Don't know that module, but likely you need to do something like

CODE

  my $count = 0;
  foreach (@$vm_views) {
    $count++ if defined $_->snapshot;
  }
  return $count;

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Newbie Question on Arrays

(OP)
Ok I've actually changed this around to simplify it for myself.

Quote:


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)
 

RE: Newbie Question on Arrays

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close