Wednesday, 15 September 2010

Formatting text file in form $number/t$q/t$a in perl -


I am trying to format the text file so that it indexes the number of questions. I have a code so far:

  my% question = map {partition (/ \ t /, $ _, 2)} @ qa_list;   

This formatted the data in a list of different questions / answers by a tab correctly. However, I can not understand how to modify it so that it can explain the number of questions. I'm thinking of nesting in another partition () or there mapping, but I give the air with complex expressions that do not work and are hard to read.

Edit: In response to the first comment,

output, at present, this will be:

  Question 1 \ tAnswer1 question 2 \ t Ansver2 question 3 \ tAnswer3   

But I should have this:

  1 question 1 \ n anncerce 1 2 question 2 \ n anncer 2 3 questions3 \ n anncer 3    

you

  question 1 & lt; Tab & gt; Answer 1 question 2 & lt; Tab & gt; Answer 2 ... want to take an array containing   

and print out

  1 & lt; Tab & gt; Question 1 & lt; Tab & gt; Answer 1 2 & lt; Tab & gt; Question 2 & lt; Tab & gt; Answer 2 ...   

So this is just the question of adding array index plus one and one tab to the front.

  print "$ _ \ t $ qa_list [$ _- 1] \ n" for 1 .. @qa_list;    

No comments:

Post a Comment