[perl]How to treat string like "a b" as a single string when split?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What "it" ? What exactly is not clear ? I.e. which is the first sentence you do not understand and what exactly in that sentence you do not understand ?
The document begins with one-two-line examples, e.g.:
Code:
# Extract the initial substring of $text that is delimited by
# two (unescaped) instances of the first character in $delim.
($extracted, $remainder) = extract_delimited($text,$delim);
just my first question, I've tried some code, but cannot do what i want..
I hardly can understand the manual
#!/usr/bin/perl -w
use strict;
use Text::Balanced qw (
extract_delimited
extract_bracketed
extract_quotelike
extract_codeblock
extract_multiple
);
my $text = 'a b "c d" [e f] g';
my ($extracted, $remainder) = extract_bracketed( $text, '[' );
print $extracted."\n";
print $remainder."\n";
Last edited by john.daker; 06-01-2009 at 04:08 AM.
just my first question, I've tried some code, but cannot do what i want..
I hardly can understand the manual
#!/usr/bin/perl -w
use strict;
use Text::Balanced qw (
extract_delimited
extract_bracketed
extract_quotelike
extract_codeblock
extract_multiple
);
my $text = 'a b "c d" [e f] g';
my ($extracted, $remainder) = extract_bracketed( $text, '[' );
print $extracted."\n";
print $remainder."\n";
here's what i do with Python, use it as guide to write the Perl equivalent. The algorithm is easy to understand. Note this is only simplistic case.
Code:
f=0 # declare a flag
# s = 'a b "c d" [e f] g {h i j k} l m' #test string
s = 'a b "c d" [e f] g {h i j k} l m [n o p q r s ] t u v' #test string
items = s.split() #split on space
punct = ['"',"[","]","{","}"] #get a list of relevant punctuations
for i in items: #iterate the splitted items
if i[-1] in punct: #check if last character is punctuation.
print i
f=0
continue #use "next" keyword in Perl
if i[0] in punct and not i[-1] in punct: #check things like "c or [e
f=1
if f:
print i, #print without newline (side by side)
if f==0:
print i #print when no punctuation.
output
Code:
# ./test.py
a
b
"c d"
[e f]
g
{h i j k}
l
m
[n o p q r s ]
t
u
v
Last edited by ghostdog74; 06-01-2009 at 05:04 AM.
Ok, I dont know..
I need the code to split a b "c d" [e f] g to
a
b
"c d"
[e f]
g
using Text::Balanced, if you would like to show me and I appriciate it.
I cannot do this, i'm to new to perl
Last edited by john.daker; 06-01-2009 at 05:08 AM.
Ok, I dont know..
I need the code to split a b "c d" [e f] g to
a
b
"c d"
[e f]
g
using Text::Balanced, if you would like to show me.
I cannot do this.
If you enable diagnostics, you'll see what the problem with your code is.
If you (re)read the piece on prefixes, you'll see that "you have been warned", i.e. your original code couldn't work by construction.
I can show you my code, but the point is that I have read the document in order to understand why your code doesn't work, and you apparently haven't.
As many people have said here, the point is not to bring you fish, the point is to teach you to fish, so in this case reading mentioned above portions of the document and acting accordingly is part of the process.
@ Sergei: I understand that you are following the style you prefer (teach a man to fish...), but the OP is obviously new to Perl, and frankly Text::Balanced is a hideously complicated module that assumes that you already have an excellent understanding of regular expressions and complex text operations. As for the note about prefixes, I'm relatively comfortable with Perl, and I can't see at all how it applies to the OP's problem. Would you perhaps explain what you have in mind by referring him to that note a bit more clearly?
@ Sergei: I understand that you are following the style you prefer (teach a man to fish...), but the OP is obviously new to Perl, and frankly Text::Balanced is a hideously complicated module that assumes that you already have an excellent understanding of regular expressions and complex text operations. As for the note about prefixes, I'm relatively comfortable with Perl, and I can't see at all how it applies to the OP's problem. Would you perhaps explain what you have in mind by referring him to that note a bit more clearly?
The OP posted code which doesn't work. The code doesn't work for an obvious reason which is wrong prefix.
I.e. the prefix the OP needs to make his code work is not the default prefix, and the documentation explains what the default prefix is.
I learned about Text::Balanced years ago, but the OP's code is the first case that I've used Text::Balanced.
Yet again I've found that reading documentation is useful.
In the beginning of the document one can read:
Quote:
DESCRIPTION
The various extract_... subroutines may be used to extract a delimited substring, possibly after skipping a specified prefix string. By default, that prefix is optional whitespace (/\s*/ ), but you can change it to whatever you wish (see below).
.
So, the root cause of the OP's problem is explained in the very two first sentences of the DESCRIPTION.
What else should be done if what documentation says is simply ignored ?
And the ignored items are not at the end, but in the beginning.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.