spikey_wan has asked for the wisdom of the Perl Monks concerning the following question:

Just to let you know, I'm sorted now. Thanks to all who helped, there are some nice innovative ideas in the answers.
Spike.

Hello World!

This question sounds simple, but when you look into it, you'll realise that there's more to it than first appears. If you run a script from the DOS command line with arguments, the arguments go into @ARGV in EXACTLY the right manner for my application.

Example:
my-script.pl one two "three four" five
will give an @ARGV array like this:
one, two, three four, five

I am trying to replicate this with a GUI, getting the information with a TK Entry widget. Unfortunately the widget will not allow me to assign it's data to an array, so I need to convert the data from it's string to an array.

my $entry=$mw->Entry(-textvariable=>\$string)->pack();
If I use split, like this:
@array = split (' ', $string);
Then a string of:
one two "three four" five
becomes
one, two, "three, four", five

I did start looking at looping through this incorrect array, and creating a new 'corrected' one, but when I realised what this entails, I found it was getting a little close to my limit, especially when I worked out all the considerations that need making. This is the sort of string it would have to cope with:
one "two three" fo"ur "five si"x seven "eight
Giving an array of:
one, two three, fo, ur , five, si, x seven , eight

Any help would be much appreciated.

S.

Edit by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Convert a string to an array?
by halley (Prior) on Mar 31, 2004 at 15:49 UTC
    Text::Balanced will help you grab the appropriate quoted strings, and the rest are pretty straightforward tokens. See the bit about extract_multiple() for an example. This may be enough for your needs, but matching the command line exactly can be difficult.

    The Win32 CMD.EXE's command line parsing of arguments is pretty abysmal, but you should be able to replicate it if you consider the way it's implemented. Hint: split and regex may just get you into trouble unless you're careful. Grow an @ARGV one character at a time.

    One thing that's in your favor is that you probably don't care about environment %variable% interpolation.

    --
    [ e d @ h a l l e y . c c ]

Re: Convert a string to an array?
by Fletch (Bishop) on Mar 31, 2004 at 16:11 UTC
Re: Convert a string to an array?
by dreadpiratepeter (Priest) on Mar 31, 2004 at 15:50 UTC
    Try looking at Text::CSV_XS. It does all the parsing you want, and more.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Convert a string to an array?
by Crackers2 (Parson) on Mar 31, 2004 at 16:29 UTC
    If you want your widget to behave exactly like the command line, you should be aware that your parsing of your last example is incorrect.

    one "two three" fo"ur "five si"x seven "eight

    will get parsed by the command line as

    one, two three, four five, six seven eight

    because a quote is not an argument delimiter. Your example is equivalent to the following:

    one two\ three four\ five six\ seven\ eight

Re: Convert a string to an array?
by Roy Johnson (Monsignor) on Mar 31, 2004 at 15:49 UTC