This seemed like an interesting challenge. The following works assuming swift knows how many elements you have. However, there are many simpler things I would /think/ should work that do not. There seem to possibly be some bugs (i.e. things that don't seem intentional) about declaring a variable before an iterate statement and then writing to it on each iterate statement. Hence the use of lists below. I will also put a second version after which I would think should work but does not<div>
<br></div><div>Working:</div><div><br></div><div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
$ swift test.swift <br>Swift svn swift-r3826 cog-r2988<br>RunID: 20120403-1509-3xnsrx10<br>Progress:<br>SwiftScript trace: this:is:a:test<br>Final status:</blockquote></div><div><br></div><div><br></div><div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
(string j[]) join(string s[], string c, int n){<br> j[0]="";<br> iterate i {<br> if(i<n-1){<br> j[i+1]=@strcat( j[i], s[i]+c );<br> }<br> else{<br> j[i+1]=@strcat( j[i], s[i] );<br>
}<br> } until(i==n-1);<br>}<br>string a[];<br>a[0] = "this";<br>a[1] = "is";<br>a[2] = "a";<br>a[3] = "test";<br>int nelements=4;<br>string j[];<br>j = join( a, ":", nelements );<br>
trace(j[nelements]);</blockquote><div><br></div><div>Not working (error, jlist has multiple writers):</div><div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
(string j) join(string s[], string c, int n){<br> string jlist[];<br> jlist[0] = "";<br> iterate i {<br> if(i<n-1){<br> jlist[i+1]=@strcat( jlist[i], s[i]+c );<br> }<br> else{<br>
jlist[i+1]=@strcat( jlist[i], s[i] );<br> }<br> } until(i==n-1);<br> j = jlist[n];<br>}<br>string a[];<br>a[0] = "this";<br>a[1] = "is";<br>a[2] = "a";<br>a[3] = "test";<br>
int nelements=4;<br>string j;<br>j = join( a, ":", nelements );<br>trace(j);</blockquote></div><div><br></div><div><br></div><br><div class="gmail_quote">On Tue, Apr 3, 2012 at 2:02 PM, David Kelly <span dir="ltr"><<a href="mailto:davidk@ci.uchicago.edu">davidk@ci.uchicago.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
Is there a way to concatenate all elements of an array into a single string? I would also like to define a separator between elements. I was thinking of something similar to Perl's join function.<br>
<br>
If I have:<br>
<br>
string a[];<br>
a[0] = "this";<br>
a[1] = "is";<br>
a[2] = "a";<br>
a[3] = "test";<br>
<br>
How can I get it into "this is a test" or "this:is:a:test"? @strcat returns a reference. I can tracef with %q and get "[this,is,a,test]", but it doesn't give me any control over the formatting as far as I know.<br>
<br>
I could call a shell script to do this, just wondering if there was another way.<br>
<br>
David<br>
_______________________________________________<br>
Swift-devel mailing list<br>
<a href="mailto:Swift-devel@ci.uchicago.edu">Swift-devel@ci.uchicago.edu</a><br>
<a href="https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-devel" target="_blank">https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-devel</a><br>
</blockquote></div><br></div>