Jose Perez wrote:
Hi!

Hello,

I've been using Perl for awhile but I still have some doubts :) hopefully
you will help me to clarify some concepts :)

In particular, I have the following situation: I have an external command
that returns the following:

Virtual Disk: 1
Virtual Disk: 6
Virtual Disk: 8
Virtual Disk: 7
Virtual Disk: 5
Virtual Disk: 3
Virtual Disk: 2
Virtual Disk: 4

What I want is to have in a list only the numbers of the virtual disks ( 1,
2, ...., 7, 8).
By trial and error, I found that the following code works:

my @virtual_disks = sort map { /Virtual Disk:\s(\S+)/ } `$SSCS_CMD list -a
$storage vdisk`;

my question is, WHY IS THIS WORKING? My first idea was to use this command
instead:

It works because a regular expression in list context returns a list of the contents of capturing parentheses.

But since you are working with numbers it would probably be better as:

my @virtual_disks = sort { $a <=> $b } map /Virtual Disk:\s+(\d+)/, `$SSCS_CMD list -a $storage vdisk`;


my @virtual_disks = sort map { s/Virtual Disk:\s(\S+)/$1/ } `$SSCS_CMD list
-a $storage vdisk`;

but this, for some reason that I don't know, doesn't work, it returns this
list (1, 1, 1, 1, 1, 1, 1, 1) instead of (1, 2, 3, 4, 5, 6, 7, 8); can
anyone explain me why?

Because the substitution operator always returns either true (1) or false ('').



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to