Jump to content

Advanced array syntax?


Recommended Posts

If I have an array called $ARRAY why cant I access the last element using $ARRAY[-1] or the second to last element using $ARRAY[-2]

I have this 2 line code to retrieve the filename from a url but I want to do it in one line:

$A = StringSplit($URL, "/")

$FILENAME = $A[$A[0]]

like

$FILENAME = StringSplit($URL, "/")[-1]

Link to comment
Share on other sites

  • Moderators

If I have an array called $ARRAY why cant I access the last element using $ARRAY[-1] or the second to last element using $ARRAY[-2]

Personally, it wouldn't make much sense to me if it were.

I have this 2 line code to retrieve the filename from a url but I want to do it in one line:

$A = StringSplit($URL, "/")

$FILENAME = $A[$A[0]]

like

$FILENAME = StringSplit($URL, "/")[-1]

What difference does it make if you do it on 1 line or 2?

With a little creativity you could probably solve your issue on your own though?

$URL = "http://www.autoitscript.com/forum/index.php?act=Post&CODE=02&f=2&t=48900&qpid=368176"
$FILENAME = _StringSplitEx($URL, '/', 0, -1)
MsgBox(0, '', $FILENAME)

Func _StringSplitEx($sString, $vDelim, $iFlag = 0, $nUBound = -1)
    Local $aSplit = StringSplit($sString, $vDelim, $iFlag)
    If $nUBound > -1 Then Return $aSplit[$nUBound]
    Return $aSplit[($aSplit[0] + $nUBound]
EndFuncoÝ÷ ØGb´v¶âÆØ^~éܶ*'¶é¨­ëÇ°+-áÝaz¼ÊÚqÊ&¦X¬Z·*.Á©í¢z®¢Ú'z«¨¶X§{
.Ù÷öׯzx§¾oy©Ú®¶²jëh×6$URL = "http://www.autoitscript.com/forum/index.php?act=Post&CODE=02&f=2&t=48900&qpid=368176"
$FILENAME = StringTrimLeft($URL, StringInStr($URL, '/', 0, -1)
MsgBox(0, '', $FILENAME)
Edited by SmOke_N

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...