> In my test code i want to split a math sentence in terms, like the
> next string example:
> "(2+a)*d+(a*b-3)-4-q/2"
>
> My function split_in_terms, is a very bad implementation (i know) that
> should split the string into 4 terms, and if not possible, return
> NULL.
> For that i have tryed with a List, but when using nth_data it prints
> this string: "g_list_nth_data", only once. So i think that Lists are
> not yet fully implemented so i switched to Array.
> Where now i cant compile it.
> ERROR:arraylist.c:322:gee_array_list_real_get: assertion failed: (_tmp0)
>
> And i dont have any clue where the problem is.

I find that the Gee collections are the nicest to use with Vala. (See
attachment.)
http://live.gnome.org/Vala/Tutorial#head-2eb5f4c78cb61f38bd03db07f37abf55a9ec4c07

> Also i dont know when should i use pointers in Vala.
> I know from C that every object must be defined as a pointer, but the
> Vala bindings, doesnt defines them as pointer, so, why should i add an
> *

Pointers should probably be avoided whenever possible.
http://live.gnome.org/Vala/Tutorial#head-8712c308df0fc07f772bb1f8011373900d8a46ac

> If i dont add it, i cant change the value of the parameter.
> supouse a function like next:
> private static void split_in_terms(string strmath, string* term_left,
> string* term_right)
> This also gives a lot of warnings  for term_left and right.

Vala supports the modifiers "out" and "ref" for passing arguments by reference.
http://live.gnome.org/Vala/Tutorial#head-64f47493a7181152df5d9db98a2f998ef6147163

-- 
http://homes.eff.org/~barlow/EconomyOfIdeas.html
http://www.dreamsongs.com/MobSoftware.html
http://www.gnu.org/philosophy/shouldbefree.html
using GLib;
using Gee;

// valac --pkg gee-1.0 -o blah blah.vala
public class Foo: Object {
	public static int main() {
		Gee.List<string>? terms = split_in_terms ("(2+a)*d+(a*b-3)-4-q/2");

		if(null == terms) {
			stdout.printf("Error parsing string!\n");
			return 1;
		}

		foreach (string term in terms) {
			stdout.printf("Got term \"%s\"\n", term);
		}

		return 0;
	}

	private static Gee.List<string>? split_in_terms(string strmath)
	{
		int x = 0;
		int parentesis=0;
		ArrayList<string> list = new ArrayList<string>();
		string str = strmath;
		StringBuilder term = new StringBuilder("");
		int p = 0;

		stdout.printf("str = %s\n", str);

		for(weak string s = str; s.get_char()!=0 ; s = s.next_char()) {
			unichar c = s.get_char();
			if ( c == '(') {
				parentesis++;
				term.append_unichar(c);
			} else if ( c == ')') {
				parentesis--;
				term.append_unichar(c);
			}
			else if ( c == '+') {
				if ( parentesis == 0) {
					list.add( term.str );

					print("%s\n",term.str);
					term = new StringBuilder("");
					p = x+1;
					if ( str.len() >= p )
						list.add( "+" );
				}
			} else {
				term.append_unichar(c);
			}
			x++;
		}
		list.add( term.str );

		if (parentesis != 0) {
			 stdout.printf("SintaxERROR: Los parentesis no cierran adecuadamente: %s\n", str);
			 return null;
		}
		return list;
	}
}// Foo
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to