[chuck-users] issue adding elements to an array

Mario Buoninfante mario.buoninfante at gmail.com
Thu Mar 28 16:59:04 EDT 2019


Sorry, this is the right code that shows 'the issue':

function static void insert_1( string array[], string x, int pos )
{
    string mom_arr[0];
    if( pos < array.size() && pos >= 0 )
    {
        for( pos => int c; c < array.size(); c++ )
        {
            mom_arr << array[c];
        }
        x => array[ pos ];
        array.size( array.size() + 1 );
        for( 0 => int c; c < mom_arr.size(); c++ )
        {
            mom_arr[c] => array[ c + pos + 1 ];
        }
    }
    else if( pos == array.size() )
    {
        array << x;
    }
}

function static void insert_2( string array[], string x, int pos )
{
    string mom_arr[0];
    if( pos < array.size()  && pos >= 0 )
    {
        for( pos => int c; c < array.size(); c++ )
        {
            mom_arr.size( mom_arr.size() + 1 );
            array[c] => mom_arr[ c - pos ];
        }
        x => array[ pos ];
        array.size( array.size() + 1 );
        for( 0 => int c; c < mom_arr.size(); c++ )
        {
            mom_arr[c] => array[ c + pos + 1 ];
        }
    }
    else if( pos == array.size() )
    {
        array << x;
    }
}

["a", "b", "c"] @=> string s_1[];
insert_1( s_1, "ONE", 1 );
for( 0 => int c; c < s_1.size(); c++ )
{
    <<< s_1[c] >>>;
}
<<< "" >>>;
["a", "b", "c"] @=> string s_2[];
insert_2( s_2, "ONE", 1 );
for( 0 => int c; c < s_2.size(); c++ )
{
    <<< s_2[c] >>>;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20190328/f47b1225/attachment.html>


More information about the chuck-users mailing list