Jump to content
Sign in to follow this  
PERTU

Return highest numerical value from an alphanumeric array

Recommended Posts

PERTU

Hi,

I have an array that looks like this:

CODE

[0]|3

[1]|v1.stage

[2]|v2.stage

[3]|v5.stage

And I want to get the highest numerical value from it. So in this example I want it to return "5".

I've tried all of the following:

CODE

MsgBox(0,'Max Index String value',_ArrayMaxIndex( $avArray, 0, 1))

MsgBox(0,'Max Index Numeric value',_ArrayMaxIndex( $avArray, 1, 1))

;

MsgBox(0,'Max String value',_ArrayMax( $avArray, 0, 1))

MsgBox(0,'Max Numeric value',_ArrayMax( $avArray, 1, 1))

;

MsgBox(0,'Min Index String value',_ArrayMinIndex( $avArray , 0, 1))

MsgBox(0,'Min Index Numeric value',_ArrayMinIndex( $avArray, 1, 1))

;

MsgBox(0,'Min String value',_ArrayMin( $avArray, 0, 1))

MsgBox(0,'Min Numeric value',_ArrayMin( $avArray, 1, 0))

But none is returning 5. Anyone knows how to get 5?

Thank you,

Olivier.

Share this post


Link to post
Share on other sites
Paulie

If the format is always "vX.stage" then you can just StringSplit it by the "." and then StringTrimLeft 1 character.

And I'm not sure, But _ArraySort() my do it,

Or look at stringmid

Edited by Paulie

Share this post


Link to post
Share on other sites
evilertoaster

here's one way-

dim $array[4]
$array[0]=3
$array[1]="v1.stage"
$array[2]="v2.stage"
$array[3]="v5.stage"

MsgBox(0,"",HighestInt($array))

Func HighestInt($inArray)
    $max=0
    if not IsArray($inArray) then Return 0
    for $i=0 to UBound($inArray)-1
        $tempString = StripText($array[$i])
        if $tempString>$max then $max = $tempString
        Next
    Return $max
EndFunc
func StripText($inString)
    dim $outString
    for $i=0 to StringLen($inString)
        $char=int(StringMid($inString,$i,1))
        if $char<>0 Then $outString=$outString & $char
    Next
    Return $outString
EndFunc

Share this post


Link to post
Share on other sites
PsaltyDS

I have an array that looks like this:

[0]|3
[1]|v1.stage
[2]|v2.stage
[3]|v5.stage

And I want to get the highest numerical value from it. So in this example I want it to return "5".

A smart person would code a slick RegExp to pull the number out, but since it's just me:

dim $array[5]
$array[0]=3
$array[1]="v1.stage"
$array[2]="|v2.stage"
$array[3]="|v5.stage"
$array[4]="abc_v68.unstage"

MsgBox(0,"",HighestInt($array))

Func HighestInt($inArray)
    Local $i, $v, $max = 0
    if not IsArray($inArray) then Return 0
    for $i = 0 to UBound($inArray)-1
        $v = StringInStr($array[$i], "v")
        $inArray[$i] = StringTrimLeft($inArray[$i], $v)
        $v = StringInStr($inArray[$i], ".")
        $inArray[$i] = StringLeft($inArray[$i], $v - 1)
        if $inArray[$i] > $max then $max = $inArray[$i]
        Next
    Return $max
EndFunc

That kind of solution depends on knowing that the data format will aways be: <something> v <number> . <something>

It's just pulling whatever is between the first 'v' and the next '.'

:rolleyes:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
PERTU

In fact I wanted a 3 digit number. So for my example I want "5" to be returned as "005".

I tried something that looks like Paulie idea:

CODE

$FileList = _FileListToArray($dirstage)

$version = StringRight("00" & StringMid(_ArrayMax( $FileList, 0, 1), 2, StringInStr(_ArrayMax( $FileList, 0, 1), ".") -2), 3)

msgbox( 0, "MoveRenderset", "La version est: "&$version)

It seems to work alright, but will only work if the file keep the same structure. ie: "vX.stage".

So I'll look at evilertoaster's code to see if I can understand it, and see if it works with any file structure, not just "vX.stage".

Thanks to both of you,

Olivier.

edit: I'll also take a look at your code PsaltyDS.

Thank you.

Edited by PERTU

Share this post


Link to post
Share on other sites
PsaltyDS

In fact I wanted a 3 digit number. So for my example I want "5" to be returned as "005".

edit: I'll also take a look at your code PsaltyDS.

; Return $max
    Return StringFormat("%03d", $max)

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
SmOke_N

A smart person would code a slick RegExp to pull the number out, but since it's just me:

dim $array[5]
$array[0]=3
$array[1]="v1.stage"
$array[2]="|v2.stage"
$array[3]="|v5.stage"
$array[4]="abc_v68.unstage"

MsgBox(0,"",HighestInt($array))

Func HighestInt($inArray)
    Local $i, $v, $max = 0
    if not IsArray($inArray) then Return 0
    for $i = 0 to UBound($inArray)-1
        $v = StringInStr($array[$i], "v")
        $inArray[$i] = StringTrimLeft($inArray[$i], $v)
        $v = StringInStr($inArray[$i], ".")
        $inArray[$i] = StringLeft($inArray[$i], $v - 1)
        if $inArray[$i] > $max then $max = $inArray[$i]
        Next
    Return $max
EndFuncoÝ÷ Ù8Z¶H§vì¢[­Ýz§vÊ'z0x-«mçZµ§è®f­Â)ek²±·¥¶Ê&zØb-¾[gºfÞ®e¶Ê&zØb-"Ýý²;¬¶¥)à­z÷«ÆÞ·Ø^~*ì·oßÖ§vØ^ìmßÝýþ«¨µå,xDF¬Ê°z·¬º²Ø§æ«'«Ê¢é]r§ëazö«©í¢»¬zéí¨º·r§ºfÞ¬.¦,y«­¢+Ø¥¹±Õ±ÐíÉÉä¹ÔÌÐì)¥´ÀÌØíÉÉålÕt(ÀÌØíÉÉålÁtôÌ(ÀÌØíÉÉålÅtôÅÕ½ÐíØĹÍÑÅÕ½Ðì(ÀÌØíÉÉålÉtôÅÕ½ÐíñØȹÍÑÅÕ½Ðì(ÀÌØíÉÉålÍtôÅÕ½ÐíñØÔ¹ÍÑÅÕ½Ðì(ÀÌØíÉÉålÑtôÅÕ½Ðí}ØØà¹Õ¹ÍÑÅÕ½Ðì((ÀÌØí!¥¡ÍÐô}!¥¡ÍÑ%¹Ð ÀÌØíÉÉä°Ä°ÌäíØÌäì°ÌäìÀäÈì¸Ìäì¤)}ÉÉå¥ÍÁ±ä ÀÌØí!¥¡ÍФ()Õ¹}!¥¡ÍÑ%¹Ð ÀÌØíÙÉÉä°ÀÌØí¥   ÍôÀ°ÀÌØíÍMÑÉÐôÌäìÌäì°ÀÌØí͹ôÌäìÌäì¤(%1½°ÀÌØí¹!½±±µ9Õ´°ÀÌØí¹5á9Õ´°ÀÌØí¹QµÁ9Õ´°ÀÌØíMI(%½ÈÀÌØí¥
ôÀÌØí¥    ÍQ¼U  ½Õ¹ ÀÌØíÙÉÉ䤴Ä($$ÀÌØíMIôMÑÉ¥¹IáÀ ÀÌØíÙÉÉålÀÌØí¥

t°ÀÌØíÍMÑÉеÀìÌäì ÀäÈí¬¤ÌäìµÀìÀÌØí͹°Ä¤($%%%ÍÉÉä ÀÌØíMI¤Q¡¸($$$ÀÌØí¹QµÁ9Õ´ô%¹Ð ÀÌØíMIlÁt¤($$%%ÀÌØí¹QµÁ9Õ´ÐìôÀÌØí¹5á9Õ´Q¡¸($$$$ÀÌØí¹5á9Õ´ôÀÌØí¹QµÁ9Õ´($$$$ÀÌØí¹!½±±µ9Õ´ôÀÌØí¥
($$%¹%($%¹%(%9áÐ(%%ÀÌØí¹5á9Õ´ôÌäìÌäìQ¡¸IÑÕɸMÑÉÉ½È È°À°À¤(%1½°ÀÌØíIÑÕɹlÍtôlÈ°ÀÌØí¹5á9Õ´°ÀÌØí¹!½±±µ9Õµt(%IÑÕɸÀÌØíIÑÕɸ)¹Õ¹

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.