1

I have searched for several hours and I've found tons of resources, but I can't seem to put the info together for my purposes. I have a file with:

FirstName LastName
FirstName LastName
FirstName LastName
etc...

This is homework assigned to the class by students from their presentation. 99% of the code below is theirs from a template script, I'll comment where my contributions come in. I'm supposed to find all of the last names that begin with a vowel and output them into a text file. It seems that I'm having most of my trouble figuring out the regex for "vowel as first character of second field" - as well as some trouble with basic Perl I suppose. How do I do this? This isn't really worth much credit at all, but I'm just driving myself crazy trying to figure this out - Thanks a bunch for any help.

  #!/usr/bin/perl


  $input = 'names.txt';
  open ($info, $input) or die "Could not open $input";

  $output = 'hw.txt';
  open($fh, '>>', $output) or die "Could not open $output";

  @names = split(/\s/, $line);  #MINE - probably wrong somehow

  while( my $line = <$info>)  {

    if ($names[2] =~ /^AEIOU/){  #MINE - also probably wrong somehow

        print $fh "$line";
      }
  }



  close $info;
  close $fh;  
Shane
  • 33
  • 5
  • 1
    http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings – mpapec Dec 06 '14 at 01:20
  • I'm staring at this post, wondering if it is what I think it is or just wishful thinking...more research – Shane Dec 06 '14 at 01:40
  • Trying out what is suggested is better than wondering. – mpapec Dec 06 '14 at 01:45
  • Lol. I meant that I'm researching the topic of "use strict;" and "use warnings;" - It's not entirely apparent to me how to use those yet - Side note though: Does my problem not lie within my regex? – Shane Dec 06 '14 at 01:51
  • 2
    regex should be `/^[AEIOU]/i` but this is only part of the problem. – mpapec Dec 06 '14 at 01:53
  • Jesus. I tried so many different things... Thank you very much for getting me pointed in the right direction. The regex was the main factor driving me nuts. I figured my additions were just gobbledygook. My first time ever seeing a Perl script was probably an hour ago... – Shane Dec 06 '14 at 02:00

2 Answers2

1

Like many programming languages, Perl's array indices start at 0

GWP
  • 131
  • 2
  • Lol, yes indeed. Well, I changed it to "$names[1]", same result though - the output file is created but left empty... – Shane Dec 06 '14 at 01:38
0

You sound like you have your answer, but just for completeness you're also using $line before it's actually initialized, you don't need to re-quote "$line" in your print, and also instead of splitting the line first, you could just use a regex match from the very start:

while( my $line = <$info> ) {
  if( $line =~ /\s[AEIOU]/ ) {
    print $fh $line;
  }
}

That's actually slightly safer than your code too because if there's a double-space then your code will result in the second array element being an undef (although there are other ways to mitigate that).

As an aside, I'm very pleased to see Perl still being the subject of homework assignments :)

smathy
  • 26,283
  • 5
  • 48
  • 68
  • I see! I think that was probably what was intended anyway. Looks pretty sleek too. I'll try this out and get back. Oh yeah, and on the Perl assignment, my professor is a super old fashioned guy who's pretty much been there since the beginning with computers/programming (he said he started with punch cards)...Total genius - came to my college from Carnegie-Mellon. I guess he knows very close to literally every programming language and has even built several of his own. He's a walking computer. – Shane Dec 06 '14 at 02:33
  • 1
    Presto! It worked. Thank you - My headache is now subsiding. – Shane Dec 06 '14 at 03:06
  • ...and just for fun, here's a one-liner from the command line: `perl -ne "print if /\\s[AEIOU]/" names.txt > hw.txt` ;) – smathy Dec 06 '14 at 03:15