Hi,

I've got a .txt with 25 of numerically named lines (example). (Example):

1. First Name:
2. Last Name:
3. Age:
4. Company:
5. Quality of the service:
...
25. Would you recommend our services:

I'm trying to strip everything after the colon so I'm just left with the
answer. In case there is no answer to a question I'm trying to leave it as a
"-". Where am I going wrong? All I get is the default "-" in case no answers
is given ...

#!/usr/bin/perl
# strip_answers.pl

use strict;
use warnings;

# array to store the stripped results
my @information;

# open a filehandle to output.txt (where the results are stored) in read
mode
open (IN, '<', 'output.txt') or die "Can't open output.txt: $!\n";

# while the filehandle is open, read each line and see if it matches
# the regular expression
while (<IN>) {

# iterate though the filehandle 25 times (one for each question). I set $i
to 1 as
# the first question begins with the number 1
for (my $i=1; $i<26; $i++) {

    # see if the regexp matches
    if ($_ =~ /^$i\..+:\s(.+)/) {
        $information[$i] = $1;
    # if there is no match, set the value to "-"
    } else {
        $information[$i] = "-";
    }
 }
}

close IN or die "Can't close output.txt: $!\n";

# cycle through theh array to check the results
foreach (1..$#information) {
 print "$information[$_]\n";
}

Thanking everyone in advance.

Cheers,
Juan Manuel.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to