Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Splitting blunder

by helmex (Initiate)
on Apr 08, 2002 at 18:00 UTC ( #157495=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I am having problems splitting a string.
The string looks like this

$string=\\Server\user;
or sometimes
$string=\\Server\dir\user;

I tried to use the following statment:
@tmp=split(/\/,$string);
and
@tmp=split(/\\/,$string);
But when I try to print out elements of @tmp I get blank lines.
Does anybody have any suggestions?
%^+^%

Replies are listed 'Best First'.
Re: Splitting blunder
by Juerd (Abbot) on Apr 08, 2002 at 18:11 UTC

    $string=\\Server\user;

    Syntax error. Add quotes (And note that \\ will evaluate to a single backslash).

    @tmp=split(/\/,$string);

    Syntax error. The backslash escapes the delimiter, leaving an undelimited string.

    Does anybody have any suggestions?

    Try posting the code you're working with (but only relevant bits), instead of some broken examples. Learn about using the <code> tag too.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: Splitting blunder
by swiftone (Curate) on Apr 08, 2002 at 18:10 UTC
    1) Are you sure you aren't having issues with escaping characters?
    $string ="\\Server\user"; #WRONG! $string ='\\Server\user'; #right

    2) What does @tmp look like? Try Data::Dumper. Is $string being placed in element 0, or what?

      $string ='\\Server\user'; #right

      Even with single quotes, backslash can escape delimiters and itself. $string be \Server\user.

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: Splitting blunder
by extremely (Priest) on Apr 08, 2002 at 18:10 UTC
    $s = '\\Server\user\dir'; @tmp = split /\\+/, $s; print join(" -- ", @tmp);

    You're going to want to put a double backslash in to match a single, and you might want to add the '+' after it to match multiples inline.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Splitting blunder
by perlplexer (Hermit) on Apr 08, 2002 at 18:18 UTC
    Not sure how you can get blanks for all of them...
    The first two elements will always be blank since Server name is always preceded by '\\'...
    Here is what you could do to avoid that
    # example 1 @tmp = split /\\/, substr($string, 2); print join(',', @tmp), "\n"; # example 2 $string =~ s/^\\+//; @tmp = split /\\/, $string; print join(',', @tmp), "\n"; # example 3 @tmp = split /\\+/, $string; shift @tmp; print join(',', @tmp), "\n";

    --perlplexer
Re: Splitting blunder
by perigeeV (Hermit) on Apr 08, 2002 at 19:00 UTC

    I always run to modules when I have regexp problems:

    #!/usr/bin/perl -w use strict; use File::Basename; my $fullpath = '\\Server\dir\user'; my $popped; my @fileparts; while($fullpath ne "" && ($popped = basename($fullpath)) ) { push @fileparts, $popped; $fullpath = dirname($fullpath); } for(@fileparts) {print "$_\n"}
    It does what you want, but without splitting. I don't know if that's acceptable for you.


      Thanks for all the leads.
      TYPO - I did escape the backslashes, posted wrong sorry
      %^+^%

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-06-28 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (90 votes). Check out past polls.

    Notices?