Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array from a string.

0 views
Skip to first unread message

Richard S Beckett

unread,
Mar 31, 2004, 9:31:45 AM3/31/04
to
I've written a nice little script, and it gets it's array of data from the
command line arguments.

If you enter something like this: myscript.pl one two "three four" five

then the @ARGV array will contain:

one

two

three four

five

Which is EXACTLY what I want.

Now, as people seem to have an aversion to DOS these days, I've added a GUI
to the script, so that running it with no arguments fires up the gui
version.

I get my aray from an entry box...

my $entrybox = $mw -> Entry(-textvariable => \$array) -> pack();

The problem is I can't use an array for the text variable, so I have to
convert the string $array into @array.

If I do this:

@array = split(' ', $array);

Entering: one two "three four" five

gives @array of:

one

two

"three

four"

five

I was just trying to sort this out, when I realised it's an absolute
nightmare if I loop through @array, and try to if /^\".*\"/ etc.

Is there an easy way to do this?

Thanks,
--
R.
GPLRank +79.699


A. Sinan Unur

unread,
Mar 31, 2004, 9:41:49 AM3/31/04
to
"Richard S Beckett" <spik...@bigfoot.com.delete.this.bit> wrote in
news:c4eks5$9u5$1...@newshost.mot.com:

> I was just trying to sort this out, when I realised it's an absolute
> nightmare if I loop through @array, and try to if /^\".*\"/ etc.
>
> Is there an easy way to do this?

Yes there is. It is called checking the FAQ list before posting:

perldoc -q inside


--
A. Sinan Unur
1u...@llenroc.ude (reverse each component for email address)

Gunnar Hjalmarsson

unread,
Mar 31, 2004, 10:35:07 AM3/31/04
to
Richard S Beckett wrote:
> If I do this:
>
> @array = split(' ', $array);
>
> Entering: one two "three four" five
>
> gives @array of:
>
> one
>
> two
>
> "three
>
> four"
>
> five

See the FAQ, as Sinan suggested.

A 'regexish' application of the FAQ answer may be:

my @array;
push @array, $+ while $array =~ /"([^"]+)"|(\S+)/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Richard S Beckett

unread,
Mar 31, 2004, 10:46:36 AM3/31/04
to
> > Is there an easy way to do this?
>
> Yes there is. It is called checking the FAQ list before posting:

How do you know I didn't?

> perldoc -q inside

Now, there's a word I would _never_ have associated with this problem,
thanks.

R.


Paul Lalli

unread,
Mar 31, 2004, 11:33:40 AM3/31/04
to

Frankly, neither would have I. But perldoc -q split and perldoc -q
delimited would have given you the same thing. :-P

Paul Lalli

A. Sinan Unur

unread,
Mar 31, 2004, 11:40:31 AM3/31/04
to
"Richard S Beckett" <spik...@bigfoot.com.delete.this.bit> wrote in
news:c4ep8f$bin$1...@newshost.mot.com:

There are many ways of looking for what you need in the FAQ list. What I
gave you is a short-cut that one figures out after finding the entry for
the first time.

The first time I found that entry was by reading through perlfaq4:

DESCRIPTION
This section of the FAQ answers questions related to manipulating
numbers, dates, strings, arrays, hashes, and miscellaneous data
issues.

Hmmmm .. You would have found the answer had you looked at the table of
contents and then read perlfaq4.

Richard S Beckett

unread,
Mar 31, 2004, 12:16:53 PM3/31/04
to
> Hmmmm .. You would have found the answer had you looked at the table of
> contents and then read perlfaq4.


OK, it's a fair cop! :-) I'll try harder next time.

Thanks for the help.
--
R.
GPLRank +79.699


Tad McClellan

unread,
Mar 31, 2004, 1:39:10 PM3/31/04
to


perldoc -q string


would do it too, and the OP might have thought of that search
term since he used it in the Subject: himself. :-)


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

SoCalSam

unread,
Apr 19, 2004, 7:27:31 PM4/19/04
to
6 posts telling the poor dude to read the FAQ. Couldn't the first responder
just answer the Q, and end their post with a friendly reminder to read the
FAQ?

Jeesh.


"Richard S Beckett" <spik...@bigfoot.com.delete.this.bit> wrote in message
news:c4euhp$dfo$1...@newshost.mot.com...

Sam Holden

unread,
Apr 19, 2004, 7:42:13 PM4/19/04
to
On Mon, 19 Apr 2004 23:27:31 GMT, SoCalSam <samsc...@hotmail.com> wrote:
> 6 posts telling the poor dude to read the FAQ. Couldn't the first responder
> just answer the Q, and end their post with a friendly reminder to read the
> FAQ?

Why?

The OP can just read the FAQ themselves once they have been pointed to it.
What's the point of repeating the answer in a post?

[snip a full quote]

Please don't do that.

--
Sam Holden

Jürgen Exner

unread,
Apr 19, 2004, 8:45:28 PM4/19/04
to
[Jeopardy-posting deleted; please don't do that]

SoCalSam wrote:
> 6 posts telling the poor dude to read the FAQ. Couldn't the first
> responder just answer the Q, and end their post with a friendly
> reminder to read the FAQ?

You don't know much about how Usenet posts are propagated, do you?

jue


0 new messages