Use Lua to implement PHP's Print_r () function _lua

Source: Internet
Author: User
Tags lua

Before you wrote some PHP-like functions, here's a print_r () function with the following code:

Copy Code code as follows:

function PR (t, name, indent)
Local tablelist = {}
function Table_r (t, name, indent, full)
The local id = not full and name or type (name) ~= "number" and ToString (name) or '. Name... '] '
Local tag = indent. Id.. ' = '
Local out = {}--result
If Type (t) = = "Table" Then
If TABLELIST[T] ~= nil Then
Table.insert (out, tag. '{} -- ' .. Tablelist[t]. ' (self Reference) ')
Else
tablelist[t]= full. '.' .. ID) or ID
If Next (t) then--Table not empty
Table.insert (out, tag. '{')
For key,value in pairs (t) do
Table.insert (Out,table_r) (value,key,indent.  '| ', Tablelist[t])
End
Table.insert (out,indent. '}')
Else Table.insert (Out,tag. ' {} ') end
End
Else
Local val = type (t) ~= "number" and type (t) ~= "boolean" and "".. ToString (t) ... ' "' or ToString (t)
Table.insert (out, tag.. val)
End
Return Table.concat (out, ' \ n ')
End
Return Table_r (t,name or ' Value ', indent or ')
End
function Print_r (t, name)
Print (PR (t,name))
End

Local A = {x=1, y=2, label={text= ' Hans ', Color= ' Blue '}, list={' A ', ' B ', ' C '}}

Print_r (a)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.