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

Discussion in 'Perl Misc' started by Vijai Kalyan, Oct 16, 2004.

  1. Vijai Kalyan

    Vijai Kalyan Guest

    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.
    Vijai Kalyan, Oct 16, 2004
    #1
    1. Advertising

  2. Vijai Kalyan wrote:
    > 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
    Jürgen Exner, Oct 16, 2004
    #2
    1. Advertising

  3. Vijai Kalyan

    Vijai Kalyan Guest

    >
    > 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.
    Vijai Kalyan, Oct 17, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fily Salas
    Replies:
    4
    Views:
    501
    Fily Salas
    Apr 15, 2011
  2. David Petrou

    Q: good time to buy the camel book?

    David Petrou, Oct 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    124
    David Oswald
    Oct 13, 2003
  3. Suresh Govindachar

    Camel book: why invoke in the manner @{[&whowasi]}?

    Suresh Govindachar, Aug 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    144
    Brian McCauley
    Aug 12, 2004
  4. rihad
    Replies:
    7
    Views:
    114
    comp.llang.perl.moderated
    Oct 29, 2007
  5. Replies:
    12
    Views:
    321
    Andrew
    Jan 5, 2013
Loading...

Share This Page