Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Split problem using limit

by NovMonk (Chaplain)
on Aug 20, 2004 at 15:10 UTC ( #384613=perlquestion: print w/replies, xml ) Need Help??

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

Greetings wise brothers and sisters,

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....
if anyone cares to see it, and of course comments are welcome. I'm sure it's something very simple. Usually is. Blessings upon you all for the help, or heckling. I will take either in good grace, wise friends.

Pax,

NovMonk

Replies are listed 'Best First'.
Re: Split problem using limit
by davido (Cardinal) on Aug 20, 2004 at 15:17 UTC

    Using a limit with split tells split to return only that number of elements. However, that doesn't mean that split should skip the step of splitting. If I understand you're problem, you are trying to get split to not split, some of the time.

    Using a limit of zero won't help you. According to perldoc -f split: "If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of pop would do well to remember)." As you can see, this has nothing to do with getting split to keep the entire line in tact.

    Perhaps you should be performing whatever check you're using to determine whether or not to split, and then if the check is positive, don't invoke split on that line at all.

    Also, your first experiment (  split(0) ) can't work. You can't just leave off preceeding arguments from a subroutine and expect that Perl will know that you really intend the single arg you've provided to take the third arg's position. That falls under the category of "You can't just make sh#@ up and expect it to work..."


    Dave

Re: Split problem using limit
by eric256 (Parson) on Aug 20, 2004 at 15:25 UTC

    I don't see why you are splitting at all. $_ is holding the value of that string so wouldn't my $spectext = $_; give you your desired results? BTW all your regex should be inside / / and your data should be __DATA__ not ----DATA----. The way it is your code throws a whole bunch of errors.


    ___________
    Eric Hodges
Re: Split problem using limit
by NovMonk (Chaplain) on Aug 20, 2004 at 16:50 UTC
    Well, the "removing all doubt" appears to be covered.

    The reason the 0 worked was because split was not finding anything to split on, except for that "10 minutes" line-- it was reading the 0 as the delimiter. If I had picked another character that could never appear in any line of my text file, I would have gotten the same result-- but not because I was doing anything right. I'll stop making sh*t up, davido. And eric256's right-- if I want the whole line, why not just say so?

    Thanks to you both. I'll get back to my penance now.

    Pax,

    NovMonk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2023-12-11 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?