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

No doubt I am going to kick myself in the head when I get the answer to this questions, but here goes anyway.. ;-) I have a set of strings that I am reading from a file. The data looks like this:
SERVER1/OU=00/OU=LOCATION/O=COMPANY SERVER2/OU=00/OU=LOCATION/O=COMPANY SERVERXYZ6/OU=00/OU=LOCATION/O=COMPANY SERVERFOOBAR/OU=01/OU=LOCATION/O=COMPANY
The name of the server in each string comes before the first "/" character, and I need to extract that into another string variable. The server name is variable in length. I don't care about what comes after that first "/" character in the line. I was trying to use the split command, but I have been having a lot of problem escaping the "/"'s used to specify the character to use for the split. In addition, I get errors that appear to make no sense when I use the following, where $serverID is the string that I have read in from the file:
$cnID = split("/", $serverID); print $cnID . "\n";
Can someone give me a pointer (no pun intended) on where I am going wrong here? This seems pretty straightforward, but obviously I am doing something incorrectly. Thanks for any ideas...

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Splitting a string... Duh?
by Preceptor (Deacon) on Sep 16, 2002 at 16:17 UTC
    Presumably it's because you are trying to split and put the results into a scalar context.
    So you might want to do something like:
    $cnID = ( split("/", $serverID) )[0]; print $cnID"\n";
    (Untested, but should do it...)
      Oops, I typoed the print.
      use strict; my $cnID= (split("/", $serverID))[0]; print "$cnID\n";
      As a hint, using strict would have indicated that something was wrong with this line.
      --
      It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.
Re: Splitting a string... Duh?
by virtualsue (Vicar) on Sep 20, 2002 at 20:32 UTC
    You have two problems here. The first is the unescaped special character / as your split pattern, and the second is the use of a scalar variable in list context (already commented upon in other answers to your question).

    You need to remove the special meaning from / in order for split to be able to use it. Single quotes, which suppress interpolation, is the easiest on the eyes. You could also use "\/", /\//, m|/|, etc., consult perldoc perlre or your favorite perl reference for details.

    Next, by default split returns a list of strings. You either need to catch these with an array and then use the first item in that array ($cnID[0]), or assign your scalar $cnID to the first member of the list explicitly.

    This is similar to podmaster's answer, but keeps the list on the right hand side of the assignment:  my $cnID = (split '/', $serverID,2)[0]; The last argument to split tells split to break $serverID into no more than 2 pieces - no need to bother with any more, given that you only want the first piece anyway.

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by PodMaster (Abbot) on Sep 16, 2002 at 16:32 UTC
    You'll be wanting perldoc -f split in list context()
    my $serverID = 'serverFoo/bar/baz'; my( $cnID ) = split '/', $serverID, 2; print "$serverID $cnID";

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by Django (Pilgrim) on Sep 16, 2002 at 17:17 UTC

    Since you need just one element of the strings, you don't need to use split. Here is a simple regex solution:

    while (<DATA>) { m|(.+?)/| and push @ServerNames, $1; }

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by barrd (Canon) on Sep 16, 2002 at 16:16 UTC
    You need to split into an array:
    $serverID = "SERVER1/OU=00/OU=LOCATION/O=COMPANY"; @cnID = split("/", $serverID); print $cnID[0];
    This will give you the result you expect.

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by virtualsue (Vicar) on Sep 20, 2002 at 20:01 UTC

    Re: Splitting a string... Duh?

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by SmokeyB (Scribe) on Sep 27, 2002 at 16:03 UTC
    After you Split the file into the array, you can easily print by using a foreach loop.
    @cnID = split("/", $serverID); foreach $temp (@cdID) { print $temp . "\n"; }
    This should print out everything from the array.

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by KenShackelford (Initiate) on Sep 18, 2002 at 17:40 UTC
    Thanks to all who answered. I knew that I was close, but that only counts in horseshoes I guess. I also got some good alternative coding suggestions from some of you, too, so again, my thanks and appreciation to all of you for helping out a newbie here. Now I just need to sit down and start reading the perldocs for fun - might help me to remember something next time I get one of these little gems of a problem. ;-)

    Originally posted as a Categorized Answer.

Re: Splitting a string... Duh?
by logan (Curate) on Nov 12, 2002 at 22:25 UTC
    Presumably, you'll want to do this for every line in the file. I'm also going to guess that the list of servers will need to be read into an array. This oughta do it.

    @file = ( "SERVER1/OU=00/OU=LOCATION/O=COMPANY", "SERVER2/OU=00/OU=LOCATION/O=COMPANY", "SERVERXYZ6/OU=00/OU=LOCATION/O=COMPANY", "SERVERFOOBAR/OU=01/OU=LOCATION/O=COMPANY" ); <p> foreach $string(@file) { @sections = split ("/", $string); $name = $sections[0]; push @servers, $name; } foreach $machine(@servers) { print STDOUT "Machine = $machine\n"; }

    Originally posted as a Categorized Answer.