Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.



In reply to Split problem using limit by NovMonk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2023-12-05 22:34 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.