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

Question 1

by partha7
on Aug 30, 2012 at 10:52 UTC ( #990714=perlquestion: print w/ replies, xml ) Need Help??
partha7 has asked for the wisdom of the Perl Monks concerning the following question:

You have read a text file containing whitespace separated columns of numbers. Each line may contain leading and trailing whitespace. You must iterate through the file and, for each line, capture the numbers in an array. The elements of the array must not contain leading or trailing space and there may be no empty elements.

Based on the scenario above, which split command might you use in your loop to meet the specification?

Choice 1

my @fields = split / /, $string;

Choice 2

my @fields = split /\s/, $string;

Choice 3

my @fields = split /\s+/, $string;

Choice 4

my @fields = split ' ', $string;

Choice 5

my @fields = split /\s*/, $string;

Comment on Question 1
Select or Download Code
Re: Question 1
by moritz (Cardinal) on Aug 30, 2012 at 10:53 UTC
Re: Question 1
by Corion (Pope) on Aug 30, 2012 at 10:53 UTC

    Most likely your course material covers the knowledge needed to answer this exam question. I recommend studying the course material.

      He got hung up on the very first question. Somehow I don't think he's going to get very far.

      Well, at least it's encouraging to see that there are Perl courses being taught out there, and that this one encourages the use of my. On the other hand, the question doesn't include my preferred answer: read the lines into $_, so you don't have to specify any arguments to split to get the desired behavior.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

        I've been teaching Perl for 10 years now. I'm telling you, Perl is not dead! It's out there!

        There are no stupid questions, but there are a lot of inquisitive idiots.
Re: Question 1
by Anonymous Monk on Aug 30, 2012 at 11:17 UTC
    HA! Its brain-bench
Re: Question 1
by blue_cowdawg (Monsignor) on Aug 30, 2012 at 15:07 UTC
        You have read a text file containing whitespace separated columns of numbers.

    I do? Really? What a surprise. Is it a gift for me?

    fellow monk, there are better ways to ask homework questions. First step in my opinion is to try and solve the problem yourself. If it works, you've accomplished something truly great. If it crashes, burns and dies in the flames then you'll accomplish even greater things by posting a well titled and formatted question to Seekers of Perl Wisdom so that others in a similar predicament as yours can learn by it.

    At least, in my honest opinion, is how Perl Monks is supposed to work.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-28 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls