Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Use of Text::ParseWords

by blackgoat (Acolyte)
on Feb 08, 2010 at 04:28 UTC ( #821933=perlquestion: print w/ replies, xml ) Need Help??
blackgoat has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I am toatally new to perl and I was going through a tutorial and came across the following piece of code:

$_ = '"Jones, James", "Smith, Susan", Spot'; use Text::ParseWords; @names = quotewords(",", 0, $_); print join(" &", @names);
output: Jones, James & Smith, Susan & Spot

I didnt quite understand what it it does and how exactly it works.

Could anybody pls be kind enough to explain!

Thank You

Comment on Use of Text::ParseWords
Select or Download Code
Re: Use of Text::ParseWords
by Anonymous Monk on Feb 08, 2010 at 04:35 UTC
    quotewords is a function exported by Text::ParseWords (means you don't have to call it like Text::ParseWords::quotewords()))

      What?

      I did not quite understand what you mean. I did mention I'm totally new to this. Could you care to be more explicit pls.

      Thank you very much

Re: Use of Text::ParseWords
by quester (Vicar) on Feb 08, 2010 at 07:02 UTC
    Text::ParseWords is a core Perl module. You can find its documentation here at perldoc.perl.org, along with lots of other useful documentation, tutorials, and FAQ's.
Re: Use of Text::ParseWords
by 7stud (Deacon) on Feb 08, 2010 at 08:25 UTC

    You have a perl string containing this text:

    "Jones, James", "Smith, Susan", Spot

    The goal: split the string on the commas--but not where the commas are inside double quote marks.

    A natural first thought might be to use split(). However, the code:

     my @pieces = split /,/, $string;

    will split the string on every comma, giving you the pieces:

    • "Jones
    • James"
    • "Smith
    • Susan"
    • Spot

    The function quotewords() is in a module called Text::ParseWords, which is found here:

    http://perldoc.perl.org/Text/ParseWords.html

    and quotewords() accepts a list of lines and a delimiter and

    breaks those lines up into a list of words ignoring delimiters that appear inside quotes.

    In addition, if the second argument to quotewords() is false, e.g. 0, then quotewords() will strip the quote marks surrounding any of the pieces.

    By looking at the result of the join():

    Jones, James & Smith, Susan & Spot

    you can tell what quotewords() returned as the pieces:

    • Jones, James
    • Smith, Susan
    • Spot
      Thank you so much.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-07-26 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls