letters in a scalar to array

J

John E

hello.
i'd like to take the letters in a scalar and make each of
them an element in an array.
here's what i've got so far....

#!/usr/bin/perl -w
$filename='test.pl';
$filename =~ s/.pl//;
@blah=split(/^?/,$filename);
foreach (@blah) { print $_."\n"; }

however, i also get this blunderful little warning....

Quantifier unexpected on zero-length expression before
HERE mark in regex m/^? << HERE / at ./asdf.pl line 4.
t
e
s
t

any advice on how to avoid that annoying warning (besides
just removing the w flag)?

Thanks,
~johnny
 
M

Martien Verbruggen

On Mon, 30 Jun 2003 23:27:37 GMT,


What if the filename is "results.plot.pl"? It's probably safer to anchor
the expression.o

ehrm...

Or even results_plot.pl, given that that . will match anything.
$filename = s/.pl$//;

Correcting myself:

$filename = s/\.pl$//;

Martien
 
T

Tad McClellan

Martien Verbruggen said:
On Mon, 30 Jun 2003 23:27:37 GMT,


What if the filename is "results.plot.pl"? It's probably safer to anchor
the expression.

$filename = s/.pl$//;


What if the filename is "results.apl"? It's probably safer to
backslash the dot:

$filename = s/\.pl$//;

<grin>
 
M

Martien Verbruggen

What if the filename is "results.apl"? It's probably safer to
backslash the dot:

$filename = s/\.pl$//;

<grin>

Yeah.. :}

Also see my followup to myself.

I should just not quickly post before picking up the phone. Or
alternatively, I should learn to ignore ringing phones.


Martien
 
C

Carlton Brown

John E said:
hello.
i'd like to take the letters in a scalar and make each of
them an element in an array.
here's what i've got so far....

#!/usr/bin/perl -w
$filename='test.pl';
$filename =~ s/.pl//;
# Note - in the above line, if you want to exclude filenames that look
like PERl scripts, you probably really want at least /\.pl/, perhaps
even /\.pl$/, or maybe even /\.p(er)?l$/, for reasons described
elsewhere in the thread. Otherwise, disregard. Totally up to you.

# To harvest your "letters" you want something like this:
@blah = $filename =~ /([A-Za-z])/g;

That'll put all the "letters" in an array, as you asked.
(Note that for purposes of this demonstration, "letters" is taken in
the traditional sense and therefore does not include digits,
whitespace, metacharacters, kana, etc. If you believe otherwise, then
you'll want to change the character class accordingly).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top