Jump to content

setClipboard Table


.:HyPeX:.

Recommended Posts

Hello guys, i want to set to clipboard the function getCameraMatrix, but i want to make it in separate lines each value, but dont know how.

Like if i do ctrl+v and get this:

  
X: ETC 
Y: ETC 
Z: ETC 
LX: ETC 
LY: ETC 
LZ: ETC 
  

So far i have this, but cant do anything about it.. i cant output a table thought :(

  
function GetCameraPos(thePlayer,cmd) 
local X, Y, Z, LX, LY, LZ = getCameraMatrix () 
outputConsole("X:"..X) 
outputConsole("Y:"..Y) 
outputConsole("Z:"..Z) 
outputConsole("LX:"..LX) 
outputConsole("LY:"..LY) 
outputConsole("LZ:"..LZ) 
local tableCreate = {} 
tableCreate.X = "X:"..X 
tableCreate.Y = "Y:"..Y 
tableCreate.Z = "Z:"..Z 
tableCreate.LX = "LX:"..LX 
tableCreate.LY = "LY:"..LY 
tableCreate.LZ = "LZ:"..LZ 
setClipboard("".. tableCreate .."") 
end 
addCommandHandler("camera", GetCameraPos, false,false) 
  
  

Thanks

HyPeX

Link to comment

This is what you need, pretty much.

local array = { 
    hi = "hello", 
    water = "drink", 
    "123456", 
    1833 
} 
  
function splitArrayIntoLines( _array ) 
    if ( type( _array ) ~= "table" ) then return "" end 
    local splitted = "" 
    for i,v in pairs( _array ) do 
        splitted = splitted .. "\r\n" .. i .. ": " .. v 
    end 
    return splitted 
end 
  
addCommandHandler( "clipboard", 
    function( ) 
        if ( array ) and ( type( array ) == "table" ) then 
            local theReturn = splitArrayIntoLines( array ) 
            setClipboard( theReturn ) 
            outputConsole( theReturn ) 
        end 
    end 
) 

This returns the following:

1: 123456 
2: 1833 
water: drink 
hi: hello 

Edited by Guest
Link to comment

Thanks! this should work pretty much.

EDIT: Somehow im getting this:

  
function GetCameraPos(thePlayer,cmd) 
local X, Y, Z, LX, LY, LZ = getCameraMatrix () 
outputConsole("X: "..X) 
outputConsole("Y: "..Y) 
outputConsole("Z: "..Z) 
outputConsole("LX: "..LX) 
outputConsole("LY: "..LY) 
outputConsole("LZ: "..LZ) 
local tableCreate = {} 
tableCreate.X = "X: "..X 
tableCreate.Y = "Y: "..Y 
tableCreate.Z = "Z: "..Z 
tableCreate.LX = "LX: "..LX 
tableCreate.LY = "LY: "..LY 
tableCreate.LZ = "LZ: "..LZ 
local text = splitArrayIntoLines( tableCreate ) 
setClipboard(text) 
end 
addCommandHandler("camera", GetCameraPos, false,false) 
  
function splitArrayIntoLines( array ) 
    if ( type( array ) ~= "table" ) then return "" end 
    local splitted = "" 
    for i,v in pairs( array ) do 
        splitted = splitted .. "\r\n" .. i .. ": " .. v 
    end 
    return splitted 
end 
  

Output:

  
LX: LX: 2483 
X: X: 2483 
LZ: LZ: 21 
Z: Z: 21 
LY: LY: -1566 
Y: Y: -1666 

Link to comment

That's because the array value contains the key, that's why it duplicates itself. The function automatically sets the key first and then the value after it.

tableCreate.X = "X: "..X 
-- Should be 
tableCreate.X = X 

EDIT: Ah, I understand what went wrong there. It's because it sorts it by the value (max > min). Try this. The only problem with this is that it formats the array alphabetically, which causes lx-lz to become the first ones in the output.

local array = { 
    x = 1, 
    y = 2, 
    z = 3, 
    lx = 1, 
    ly = 2, 
    lz = 3 
} 
  
function splitArrayIntoLines( _array ) 
    if ( type( _array ) ~= "table" ) then return "" end 
    local splitted = "" 
    local formed = { } 
     
    for i,v in pairs( _array ) do 
        formed[ #formed + 1 ] = i .. ": " .. v 
    end 
     
    table.sort( formed, function( a, b ) return a < b end ) 
     
    for i,v in pairs( formed ) do 
        splitted = splitted .. "\r\n" .. v 
    end 
     
    return splitted 
end 
  
addCommandHandler( "clipboard", 
    function( ) 
        if ( array ) and ( type( array ) == "table" ) then 
            local theReturn = splitArrayIntoLines( array ) 
            setClipboard( theReturn ) 
            outputConsole( theReturn ) 
        end 
    end 
) 

Link to comment

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
  • Recently Browsing   0 members

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