Difference between revisions of "LlCSV2List"

From Second Life Wiki
Jump to navigation Jump to search
(Add caveat regarding commas in inventory asset names.)
 
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{LSL_Function
{{LSL Function
|func_id=196|func_sleep=0.0|func_energy=10.0
|func_id=196|func_sleep=0.0|func_energy=10.0
|func=llCSV2List|return_type=list|p1_type=string|p1_name=src
|func=llCSV2List|return_type=list|p1_type=string|p1_name=src
|func_footnote=
|func_footnote=
Do not confuse this function with the {{Wikipedia|CSV|CSV}} format, it is ''not'' the CSV format.
To convert a list into a comma-separated string use [[llList2CSV]].<br/>
|func_desc
Do not confuse this function with the {{Wikipedia|Comma-separated values|CSV}} format, it is ''not'' the CSV format.
|return_text=made from '''src''' as comma separated values.
|func_desc=This function takes a string of values separated by commas, and turns it into a list.
|spec=This function takes a string of values separated by commas, and turns it into an LSL list.
|return_text=made by parsing '''src''', a string of comma separated values.
 
|spec=
To do the opposite -- take a list and make it into a comma-separated string -- use [[llList2CSV]].
=== Vectors & Rotations ===
 
To use separators other than commas (especially if you can't predict when a user might have snuck a comma into data they supply the script), use [[llParseString2List]] instead, which allows you to specify separators other than commas.
 
 
== Rotation and Vector Strings ==
Anything between "<" and ">" is considered a single value regardless of the existence of a comma between.  
Anything between "<" and ">" is considered a single value regardless of the existence of a comma between.  


This ensures that [[vector]]s and [[rotation]]s get treated as a single value, with no additional cleanup needed afterward.
This ensures that [[vector]]s and [[rotation]]s get treated as a single value, with no additional cleanup needed afterward.


Note, though, that for every "<" there needs to be a corresponding ">" :
Note, though, that for every "<" there needs to be a corresponding ">" or it will consume the rest of the string. For example,
 
{{{!}}
<lsl>
{{LSL DefineRow||&#32;<code>llCSV2List("<>,>,a")</code>|returns <code>["<>", ">", "a"]</code>|(Second ">" is isolated)}}
llCSV2List("<>,>,a")   == ["<>", ">", "a"]; //didn't match the last ">"
{{LSL DefineRow||&#32;<code>llCSV2List("<<>,>,a")</code>|returns <code>["<<>,>", "a"]</code>|(Regularly paired)}}
llCSV2List("<<>,>,a") == ["<<>,>", "a"]//matched everything
{{LSL DefineRow||&#32;<code>llCSV2List("<<<>,>,a")</code>|returns <code>["<<<>,>,a"]</code>|(Third "<" lost its opposite one)}}
llCSV2List("<<<>,>,a") == ["<<<>,>,a"];    //didn't match the last "<"
{{!}}}
</lsl>
 
Note as well that Rotations and Vectors included in this list will be strings. You'll need to convert them back to Rotations or Vectors for other use when you haul them out of the list.
 
 
== Leading and Trailing Spaces ==
 
llCSV2List eats leading and trailing spaces from values.
 
For example:
 
list tmp = llCSV2List("first , second , third");
 
returns ["first","second","third"]


not ["first "," second "," third"]
=== Types ===


The function makes no assumptions about what the list entry types should be, all elements in the resulting list will be strings. It is important to know that the llList2* functions implicit typecasts do not work the same as explicit typecast. The following table gives code examples for each type that will yield the best results.


== Blank Lists ==
{{{!}} {{Prettytable|style=margin-top:0;}}
* llCSV2List("") returns [""], not []
{{!}}- {{Hl2}}
! Target
! Code
! Description
{{!}}-
{{!}}[[integer]]
{{!}}<code>(integer)[[llList2String]](mlist, mint)</code>
{{!}}[[llList2Integer]] does not support the hex format
{{!}}-
{{!}}[[float]]
{{!}}<code>(float)[[llList2String]](mlist, mint)</code>
{{!}}[[llList2Float]] does not support the scientific or hexadecimal notations
{{!}}-
{{!}}[[string]]
{{!}}<code>[[llList2String]](mlist, mint)</code>
{{!}}Always Safe
{{!}}-
{{!}}[[key]]
{{!}}<code>[[llList2Key]](mlist, mint)</code>
{{!}}Always Safe
{{!}}-
{{!}}[[vector]]
{{!}}<code>(vector)[[llList2String]](mlist, mint)</code>
{{!}}[[llList2Vector]] will return a zero vector
{{!}}-
{{!}}[[rotation]]
{{!}}<code>(rotation)[[llList2String]](mlist, mint)</code>
{{!}}[[llList2Rot]] will return a zero rotation
{{!}}}


=== Whitespace ===


llCSV2List consumes the first leading space from all values :


|caveats=* As mentioned above under the "Rotation and Vector Strings" section, all items in the returned list (including integers, floats, etc, as well) are strings until [[typecast]] (converted) otherwise by you.
<syntaxhighlight lang="lsl2">list tmp = llCSV2List("first , second , third");
//returns ["first ","second ","third"]
//not ["first "," second "," third"]</syntaxhighlight>
|caveats=
* If a "<" does not have a matching ">", the remainder of the string will be a single value, even if the "<" is in the middle of the value, see [[#Vectors & Rotations|Vectors & Rotations]] for further details.
* All items in the returned list are strings.
* If an empty string is parsed, the result will a list containing an empty string: [""] (not an empty list).
* When used with lists that are generated based off assets in inventory, [[llCSV2List]] should not be used, as inventory assets can contain commas as part of their names.
|constants
|constants
|examples=
|examples=
<lsl>
<syntaxhighlight lang="lsl2">default
default
{
{
     state_entry()
     state_entry()
Line 59: Line 77:
         llOwnerSay("CSV: " + csv);
         llOwnerSay("CSV: " + csv);
         integer i;
         integer i;
         for (i=0; i<llGetListLength(my_list); ++i)
        integer num_list=llGetListLength(my_list);
         for (i=0; i<num_list; ++i)
         {
         {
             llOwnerSay("my_list[" + (string)i + "]: " + llList2String(my_list,i));
             llOwnerSay("my_list[" + (string)i + "]: " + llList2String(my_list,i));
         }
         }
     }
     }
}</lsl>
}</syntaxhighlight>
|helpers
|helpers
|also_functions={{LSL DefineRow||[[llList2CSV]]|}}
|also_functions={{LSL DefineRow||[[llList2CSV]]|}}
Line 73: Line 92:
|also_tests
|also_tests
|also_articles={{LSL DefineRow||[[Typecast]]|}}
|also_articles={{LSL DefineRow||[[Typecast]]|}}
|notes
|notes=To use separators other than commas (especially if you can't predict when a user might have sneaked a comma into data they supply the script), use [[llParseString2List]] instead, which allows you to specify separators other than commas. [[llParseString2List]] unfortunately does not support the special parsing required for handling rotations and vectors, nor does it consume leading and trailing whitespace.
|permission
|permission
|negative_index
|negative_index

Latest revision as of 04:15, 4 July 2022

Summary

Function: list llCSV2List( string src );

This function takes a string of values separated by commas, and turns it into a list.
Returns a list made by parsing src, a string of comma separated values.

• string src

To convert a list into a comma-separated string use llList2CSV.
Do not confuse this function with the "Wikipedia logo"CSV format, it is not the CSV format.

Specification

Vectors & Rotations

Anything between "<" and ">" is considered a single value regardless of the existence of a comma between.

This ensures that vectors and rotations get treated as a single value, with no additional cleanup needed afterward.

Note, though, that for every "<" there needs to be a corresponding ">" or it will consume the rest of the string. For example,

•  llCSV2List("<>,>,a") returns ["<>", ">", "a"] (Second ">" is isolated)
•  llCSV2List("<<>,>,a") returns ["<<>,>", "a"] (Regularly paired)
•  llCSV2List("<<<>,>,a") returns ["<<<>,>,a"] (Third "<" lost its opposite one)

Types

The function makes no assumptions about what the list entry types should be, all elements in the resulting list will be strings. It is important to know that the llList2* functions implicit typecasts do not work the same as explicit typecast. The following table gives code examples for each type that will yield the best results.

Target Code Description
integer (integer)llList2String(mlist, mint) llList2Integer does not support the hex format
float (float)llList2String(mlist, mint) llList2Float does not support the scientific or hexadecimal notations
string llList2String(mlist, mint) Always Safe
key llList2Key(mlist, mint) Always Safe
vector (vector)llList2String(mlist, mint) llList2Vector will return a zero vector
rotation (rotation)llList2String(mlist, mint) llList2Rot will return a zero rotation

Whitespace

llCSV2List consumes the first leading space from all values :

list tmp = llCSV2List("first , second , third");
//returns ["first ","second ","third"]
//not ["first "," second "," third"]

Caveats

  • If a "<" does not have a matching ">", the remainder of the string will be a single value, even if the "<" is in the middle of the value, see Vectors & Rotations for further details.
  • All items in the returned list are strings.
  • If an empty string is parsed, the result will a list containing an empty string: [""] (not an empty list).
  • When used with lists that are generated based off assets in inventory, llCSV2List should not be used, as inventory assets can contain commas as part of their names.
All Issues ~ Search JIRA for related Bugs

Examples

default
{
    state_entry()
    {
        string csv = "first,second,third";
        list my_list = llCSV2List(csv);
        llOwnerSay("CSV: " + csv);
        integer i;
        integer num_list=llGetListLength(my_list);
        for (i=0; i<num_list; ++i)
        {
            llOwnerSay("my_list[" + (string)i + "]: " + llList2String(my_list,i));
        }
    }
}

Notes

To use separators other than commas (especially if you can't predict when a user might have sneaked a comma into data they supply the script), use llParseString2List instead, which allows you to specify separators other than commas. llParseString2List unfortunately does not support the special parsing required for handling rotations and vectors, nor does it consume leading and trailing whitespace.

See Also

Functions

•  llList2CSV
•  llDumpList2String
•  llParseString2List
•  llParseStringKeepNulls

Articles

•  Typecast

Deep Notes

Search JIRA for related Issues

Signature

function list llCSV2List( string src );