[chuck-users] issue adding elements to an array

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


Hi,

I suspect I'm probably doing something stupid and just not realizing it,
but I don't understand why these 2 functions ("insert_1" and "insert_2")
are giving me 2 different results:

function static void insert_1( string array[], string x, int pos )
{
    string mom_arr[0];
    if( pos < array.size() - 1 && 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] >>>;
}

It seems like expanding an array with '<<' instead of using .size(int n) is
giving me some problem.
I'm on MacOS Sierra 10.12.6

Cheers,
Mario
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20190328/2f040cfe/attachment.html>


More information about the chuck-users mailing list