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

Obtaining number(s) from array 2

Status
Not open for further replies.

stevio

Vendor
Joined
Jul 24, 2002
Messages
78
Location
AU
Hi all,

I get a return value from running execution quotes
@var =`command here`;

The output is something like

G: <+> HardDiskVolume7

What I am trying to do is to capture the last number

There is newline character at the end, so I can chomp or split the array into individual elements and look at the second last element (because the last is newline)

code:

$retval = `command here`;
@char = split (//,$retval);
do something with $char[-2];

Problem 1)
This is fine if the last number is a single digit - ie length of array stays constant. How do I capture the number if it is double-digits? eg

G: <+> HardDiskVolume12

Problem 2)
Also, I can't rely on the first block of elements/text to be constant eg sometimes it might be:

G: <+> HarddiskDmVolumes\VariableServerName\Volume7

Should I do some sort of RegExp on "VolumeX", since it is the only thing that remains constant?

Cheers
Steve
 
something to start with

@data = ('G: <+> HardDiskVolume7','HardDiskVolume777', '7HardDiskVolume7');

foreach $item (@data){
$item =~/([0-9]*$)/;
push(@nData, $1);
}

foreach $item (@nData){
print "$item \n";
}
 
Assuming there isn't more to the problem than what you've described here, the following should work.
Code:
my $retval ="G: <+> HardDiskVolume7";
my $number;
$number = $1 if $retval =~ /(\d+)$/;
I used a scalar for $retval rather than an array. If you must use an array for some reason, and the number is contained in the last array element, then make it
Code:
$number = $1 if $retval[-1] =~ /(\d+)$/;
 
arcnon,

does your solution cater for different numbers on the end ie something other than 7? This is really what I am trying to capture. find out what the last number is and use it in another subroutine

if i have read your code properly, you are saying loop thru the array and search for any instance of numbers between 0-9 and print them. What does the *$ do on the line?

$item =~/([0-9]*$)/;

mikevh or arcnon, what if there is a number in the server name?

mikevh, am i correct in saying that your code looks for not only the first digit, but any other digits after that and concatenates them together?

Thanks gents
Steve

 
it caputures any number of numbers at the end of line
* as many as it can find
$ must be at the end
 
mine will only catch the last numbers foreach item of your array. I gave you complete test code.
 
My code assumes the last non-newline chars in $retval are digits and this is what is captured. Digits occurring anywhere else in $retval are not captured. Try it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top