Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

surprised by split

by akm2 (Scribe)
on Aug 07, 2001 at 22:19 UTC ( [id://102865]=perlquestion: print w/replies, xml ) Need Help??

akm2 has asked for the wisdom of the Perl Monks concerning the following question:

In the bit of code below, I would expect $apppath to equal C:\run.com, Instead $apppath equals 2. Whats up with this? How do I get the results I need? Example code would great.

$myline = "192.168.0.1|run|C:\run.com|"; ($ip, $appid, $apppath, $extra) = split(/|/, $myline); print "$apppath\n";

Edit by tye as a title of simply "split" breaks that simple search.

Replies are listed 'Best First'.
Re: split
by scain (Curate) on Aug 07, 2001 at 22:23 UTC
    Since | is a special character, perhaps you should escape it, ie, split(/\|/, $myline).

    Scott

Re: split
by dragonchild (Archbishop) on Aug 07, 2001 at 22:28 UTC
    To explain further, what's happening is that '|' is a special character. Because the regex (the stuff in //) has nothing to match on (the special character not actually meaning anything), it matches on the empty string. As there is an empty string between every single character in a string, what you just did was equivalent to split //, $myline;

    Using your previous code, I would be willing to bet that:

    $ip == '1' $appid == '9' $apppath == '2' $extra == '.'
    The rest of the line got discarded.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

(jeffa) Re: split
by jeffa (Bishop) on Aug 07, 2001 at 22:27 UTC
    scain is correct - you need to escape the pipe. Also, you need to use single quotes instead of double quotes:
    my $line = '192.168.0.1|run|C:\run.com|';
    to keep that \r from being interpolated (or you could escape it as well).

    jeffa

Re: split
by faure (Sexton) on Aug 07, 2001 at 22:58 UTC
    Update: Removed my earlier bad advice, since the pipe is definitely NOT escaped in a quoted string.
    print join "\n", split('|', "blush");
    gives
    b l u s h
    See good advice above and below. :)
      According to perlfunc, “The pattern /PATTERN/ may be replaced with an expression to specify patterns that vary at runtime.&rdquo

      So a quoted string is an expression whose value never changes, and that will be compiled into a regex each time. It offers the same issues of double-escape and shifting meanings of escapes as /$foo/ does.

Re: split
by Anonymous Monk on Aug 07, 2001 at 23:50 UTC
    You need to either escape (with a backslash) the '|' in the split expression, or use a different delimiter in the $myline assignment. The '|' has special meaning in the split expression and cannot be used "bare". Both of the following work: $myline = "192.168.0.1|run|C:\run.com|"; ($ip, $appid, $apppath, $extra) = split(/\|/, $myline); print "$apppath\n"; $myline = "192.168.0.1!run!C:\run.com!"; ($ip, $appid, $apppath, $extra) = split(/!/, $myline); print "$apppath\n";
Re: split
by Anonymous Monk on Aug 08, 2001 at 11:17 UTC
    The pipe symbol "|" is a special character. You need to escape it with a backslash "\" before you can use it inside a string. Here is the right way to do it:

    $myline = "192.168.0.1\|run\|C:\\run.com\|";
    ($ip, $appid, $apppath, $extra) = split(/\|/, $myline);
    print "$apppath\n";
    

    This will give you exactly what you want:

    C:\run.com
    

    If you don't want to bother with escaping the pipe symbol, avoid using the pipe symbol and pick another character as the field separator instead.

    roytc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://102865]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found