Modified example from Camel book doesn't work right - Please correct

V

Vijai Kalyan

Hi,

I tried a modification of the example form the Camel Book pg. 12: The
modified program is below:
------------------------------------------------------------
#!/usr/bin/perl

# to test string splitting

# sample strings. we will split each string at the "."

@strings = ("org","org.htmlencoder","org.htmlencoder.element","org.htmlencoder.element.impl");

foreach $string (@strings) {
print "Processing string: ", $string, "$!\n";
@splitstring = split(".",$string);
foreach $component (@splitstring) {
print "Current component: ", $component, "$!\n";
$components{$component} = $component;
}
}

$index = 1;
foreach $component (sort keys %components) {
print "Component $index is: ", $component;
$index++;
}
----------------------------------------------------------

If I am right, this should produce the following output:

Processing string: org
Processing string: org.htmlencoder
Current component: org
Current component: htmlencoder
....

----------------------------------------------------------

But the only o/p I get is the "Processing string: " statements.

I am using Active Perl 5.8 alongwith the Open Perl IDE.

Suggestions?

thanx,

-vijai.
 
J

Jürgen Exner

Vijai said:
I tried a modification of the example form the Camel Book pg. 12: The
modified program is below:
------------------------------------------------------------
#!/usr/bin/perl

# to test string splitting

# sample strings. we will split each string at the "."

@strings =
("org","org.htmlencoder","org.htmlencoder.element","org.htmlencoder.element.impl");

foreach $string (@strings) {
print "Processing string: ", $string, "$!\n";
@splitstring = split(".",$string);
foreach $component (@splitstring) {
print "Current component: ", $component, "$!\n"; [...]

But the only o/p I get is the "Processing string: " statements.

You missed that the first parameter of split is a RE.
Using a dot as you are effectively splitting on every single character,
leaving nothing behind that could be returned by split() as components.

Escaping the dot like in
@splitstring = split("\.",$string);
doesn't work eitrher, because the double quotes interpolate the text into a
single dot which in turn is interpreted as an RE again.

You either have to use
@splitstring = split("\\.",$string);
or single quote the single escaped dot
@splitstring = split('\.',$string);
or much, much better: if it's used as an RE then type it like an RE
@splitstring = split(/\./,$string);
The main advantage is that you are not being fooled into believing the first
parameter were a plain string.

jue
 
V

Vijai Kalyan

You missed that the first parameter of split is a RE.
Using a dot as you are effectively splitting on every single character,
leaving nothing behind that could be returned by split() as components.

Escaping the dot like in
@splitstring = split("\.",$string);
doesn't work eitrher, because the double quotes interpolate the text into a
single dot which in turn is interpreted as an RE again.

You either have to use
@splitstring = split("\\.",$string);
or single quote the single escaped dot
@splitstring = split('\.',$string);
or much, much better: if it's used as an RE then type it like an RE
@splitstring = split(/\./,$string);
The main advantage is that you are not being fooled into believing the first
parameter were a plain string.

jue

Ah I see. I guess, I guess the mistake was I tried everything but
lookup the docs for split. I didn't know that split took an regexp but
since I am just starting with perl I doubt I would have seen the
significance immediately.

Thanx for the explanataion.

Regards,

-vijai.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top