Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Help with Text::ParseWords

by basicdez (Pilgrim)
on Oct 18, 2001 at 18:08 UTC ( #119682=perlquestion: print w/replies, xml ) Need Help??

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

Here is my code...
#!/usr/bin/perl-Tw use strict; use Text::ParseWords; open(DATA,"textfile.dat") || die "Cannot open datfile: $!\n"; my @data; my $i = 0; while (<DATA>) { chomp; last if /^"EOS"$/; { @data = &quotewords('\s+', 0, q(DATA)); } foreach (@data) { print "$i: <$_>\n"; $i++; } }
Here is textfile.dat...
"PER" "A1" "Desiree" "Leppala"01261907 999-99-9999 "ADR" "A1" "264 E Dekora St NO 1" "Saukville""WI" 53080 "EOS"
What I would expect would be output that looks as follows...
0, PER 1, A1 2, Desiree 3, Leppala ... 13, 53080
I know that I am incredibly misguided here, but please help me with as little criticism as possible. humbly dez L

Replies are listed 'Best First'.
Re: Help with Text::ParseWords
by davorg (Chancellor) on Oct 18, 2001 at 18:18 UTC
(tye)Re: Help with Text::ParseWords
by tye (Sage) on Oct 18, 2001 at 20:05 UTC

    Note also that your second line's third argument will be 'Leppala01261907' and not 'Leppala'. Text::ParseWords follows shell quoting a argument-splitting rules which means that you can construct a single argument like so: "Don't split on theses spaces "and' let me use "quotes" here' which will get you a single argument with a value of: q(Don't split on these spaces and let me use "quotes" here)

            - tye (but my friends call me "Tye")
Re: Help with Text::ParseWords
by Mask (Pilgrim) on Oct 18, 2001 at 19:30 UTC
Re: Help with Text::ParseWords
by nardo (Friar) on Oct 18, 2001 at 19:59 UTC
    In addition to the two corrections by others, your textfile.dat is not in a suitable format. You are telling quotewords that you want to use spaces as a delimiter, but your textfile.dat has records which aren't seperated by spaces "Leppala"01261907 does not have a space in it. "Saukville""WI" doesn't either which will result in you getting Leppala01261907 and SaukvilleWI as records.
      Thanks. I have resolved that problem and actually have it working now. For future reference though, do you have any suggestions on how to seperate "Leppala"01261907 or "Saukville""WI" If not though that is cool as I have it working properly now. peace dez L
        If you are unconcerned about escaped quotes ("George \"The Man\" Washington") a quick way would be to set $keep to 1
        &quotewords('\s+', 1, $_);
        and then split on quotes.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://119682]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-07-04 15:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found