Robert Krueger wrote: > Hi, > I bought 3 books a few days ago on Perl, and things are going well with > one exception. > > An example: > > @line = "this is a very long line word1+word2+word3 and it contines on and on"; > > The object is to execute a split using "+" so I end up with word1, word2, > and word3, so I do this: > > my @line = split('\+'); > > According to the books, I should end up with this: > $line[0] = word1 > $line[1] = word2 > $line[3] = word3 > > Instead it ends up to be this: > $line[0] = this is a very long line word1 > $line[1] = word2 > $line[3] = word3 and it contines on and on > > I can't figure out what I'm doing wrong. > Can someone explain?
Hello Robert You're misreading or misunderstanding your book I'm afraid. split() will simply split its second parameter (which is $_ by default if you don't specify one) into chunks at places that match its first parameter (which is actually a regular expression); nothing cleverer than that. So you can see that it has done its job correctly. By the way you need a dollar sign instead of an at on that line $line = "this is a very long line ..."; It's hard to see what the author could have intended, as its awkward to get that result from the string you've shown in a single step. Perhaps something like this will help you see where you're going wrong. It first splits the string at each section of whitespace, which is what split() does by default, and then takes the seventh item (the string 'word1+word2+word3') and splits that at the plus signs, giving you the result you expected. This is probably nothing like the book's author intended, but may lead you back onto the right track. Good luck. Rob use strict; use warnings; $_ = "this is a very long line word1+word2+word3 and it contines on and on"; my @line = split; my @words = split /\+/, $line[6]; print "$_\n" foreach @words; *OUTPUT* word1 word2 word3 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>