Grupy dyskusyjne Google nie obsługują już nowych postów ani subskrypcji z Usenetu. Treści historyczne nadal będą dostępne.

Array from a string.

0 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Richard S Beckett

nieprzeczytany,
31 mar 2004, 09:31:4531.03.2004
do
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

nieprzeczytany,
31 mar 2004, 09:41:4931.03.2004
do
"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

nieprzeczytany,
31 mar 2004, 10:35:0731.03.2004
do
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

nieprzeczytany,
31 mar 2004, 10:46:3631.03.2004
do
> > 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

nieprzeczytany,
31 mar 2004, 11:33:4031.03.2004
do

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

nieprzeczytany,
31 mar 2004, 11:40:3131.03.2004
do
"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

nieprzeczytany,
31 mar 2004, 12:16:5331.03.2004
do
> 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

nieprzeczytany,
31 mar 2004, 13:39:1031.03.2004
do


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

nieprzeczytany,
19 kwi 2004, 19:27:3119.04.2004
do
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

nieprzeczytany,
19 kwi 2004, 19:42:1319.04.2004
do
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

nieprzeczytany,
19 kwi 2004, 20:45:2819.04.2004
do
[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


Nowe wiadomości: 0