NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
As the saying goes, "It is better to remain silent and be thought a fool, than to speak up and remove all doubt." I'm going to go for it anyway, though. My problem is this:
I am creating a utility to parse a simple text file. I need to capture the text on certain lines into a single scalar variable, so I thought to use split thus:
($spectext) = split (0);
where the 0 is the Limit. Fine, but when the text on one of my lines is this: 10 minutes or less, my variable for that line comes back as a "1". All the other lines come back as expected. I get that when it sees the 0 in the line, it assumes it's the delimiter instead of the default whitespace. I tried the following,($spectext) = split (" ",$_,0); which, from my handy copy of Perl by Example, looks like it should work, (specifying delimiter,string and limit) but now all I get is the first word of every line, which is not at all what I want. My code is here
#!/usr/bin/perl use strict; use diagnostics; use warnings; my ($qnum,$qtext,$spectext,$p); while (<DATA>){ if (^[0-9]\.|^[0-9][0-9]\.|^[0-9][A-Za-z]\.|^[0-9][0-9][A-Za-z]\.){ $p = 1; ($qnum,$qtext) = split (/\./); print "L Q$qnum\nttlQ$qnum. $qtext\nn10base - total respondents\n" +; } elsif (^[A-Z]|^[0-9]){ ($spectext) = split (0); chomp $spectext; print "n01$spectext;c=c{Q$qnum}'$p'\n"; $p++; } else { print; } } ----DATA---- 1. Question text? 2 minutes or less 3 to 4 minutes 5 to 9 minutes 10 minutes or more 2. Question text? Excellent Very good Good Fair Poor ----Desired output------ L Q1 ttlQ1. Question text? n10Base - Total Respondents n012 minutes or less;c=c{Q1}'1' n013-4 minutes;c=c{Q1}'2' etc....
Pax,
NovMonk
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Split problem using limit
by davido (Cardinal) on Aug 20, 2004 at 15:17 UTC | |
Re: Split problem using limit
by eric256 (Parson) on Aug 20, 2004 at 15:25 UTC | |
Re: Split problem using limit
by NovMonk (Chaplain) on Aug 20, 2004 at 16:50 UTC |