Juan Manuel Casenave wrote:
> 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
        You are working too hard. For each question, you already have the 
index(1 thru nn), so you don't need to go through this 25 times each time.
        Also don't need to have $_ =~ since unless you override that is what is 
used. So you could do something like:

here is a small script pulling a null field on 1:
#!perl

use strict;
use warnings;
use Data::Dumper;

$_ = '1. First Name:';
my @information = ();

if ( /^(\d{1,2})\..+:\s*(.{0,})/ ) {
    $information[$1] = $2;
    if ( $2 eq '' ) {
       $information[$1] = '-';
     }
 }

print Dumper([EMAIL PROTECTED]);

Output:
[C:/CurrWrka/00CommonPerl] aapl006f
$VAR1 = [
          undef,
          '-'
        ];
[C:/CurrWrka/00CommonPerl]

I have done the trivial case of no data. You only need verify that it will work 
with data.

Wags ;)

> 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.



*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************


--
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