dheenaalex,
I am not trying to be nor do I want to be rude to you. But the code you posted clearly shows you have no idea what you are doing and have yet to understand even some very basic concepts and how some functions work, and etc.
What you are attempting to do is actually not all that easy and requires a good understanding of regular expressions. You don't yet understand how the much simpler tr/// operator works.
The obvious way to go about this is to split the string using the space between the words as the split delimiter. Then you would loop through the list that "split" returns and check if each word started with a double-quote and ended with a double-quote, or started with a double-quote or ended with a double-quote. The words with no qoutes will be the fall-through condition.
Ignoring all that code you posted because it really is so bad that it needs to be tossed and forgotten lest you remember any of it, and using your sample string here is a way that I think should be clear or that you can look up some of the syntax used in just about any perl reference book or online resource and understand it:
-
$string = '"first" second third "fourth" "fifth" "sixth seventh" eigth';
-
@list = split/\s+/,$string;
-
foreach my $w (@list) {
-
if ($w =~ /^"([^"]+)"$/) { # starts and ends with double-quotes
-
print "$1\n";
-
}
-
elsif ($w =~ /^"([^"]+)$/) { # only starts with a double quote
-
print "$1 ";
-
}
-
elsif ($w =~ /^([^"]+)"$/) { # only ends with a double-quote
-
print "$1\n";
-
}
-
else { # no quotes at all (fall-through condition)
-
print "$w\n";
-
}
-
}
-
Sample data often does not reflect real data very well and the above will not work for something like three words inside of double quotes:
"word word word"
And there are probably other exceptions.
Now all you need to do is add your file I/O operations to the above code to read the data in from a file and print it out to another file.
There is a better and more robust solution to this type of problem but I think the code would be too advanced for you to understand at this point judging by the code you posted. Even the above is stretching it for you if this is school work. Any good teacher will be very suspicious that you wrote this code.