On Oct 11, Juan Manuel Casenave said:

1. First Name:
2. Last Name:
3. Age:

#!/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++) {

Here's where your logic fails. Your outer while loop is reading a line; your inner for loop is doing the same thing 26 times! In addition, you're starting $i at 1, which means your setting $information[1], and never $information[0] (which is where arrays start).

How about this:

  my @info;

  while (<IN>) {
    # stop after the 25th line ($. holds line number)
    last if $. > 25;

    # get the stuff after the :
    my $answer = (split /:/, $_, 2)[1];

    # set $answer to "-" if $answer doesn't
    # have a non-whitespace character in it
    $answer = "-" if $answer !~ /\S/;

    push @info, $answer;
  }

And then you loop through it like so:

  for my $idx (0 .. $#info) {
    print $idx + 1, ": $info[$idx]\n";
  }

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

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


  • Re: Regex matching pro... Jeff 'japhy' Pinyan

Reply via email to