in reply to Vowel search
Consider the implicit suggestions in this refactoring of your Perl script.
#!/usr/local/bin/perl use strict; use warnings; use autodie qw( open close ); @ARGV == 1 or die "Usage: perl $0 file\n"; my $file = shift; open my $fh, '<:encoding(UTF-8)', $file; my @lines = <$fh>; close $fh; for my $line (@lines) { print $line if $line =~ m/[aeiou][aeiou]/i; } exit 0;
Since your script is using regular expression pattern matching, it's important that it knows the correct character encoding of the text in the input text file. I've assumed it's in the UTF-8 character encoding form of the Unicode coded character set. If it's in some other character encoding (e.g., Windows-1252), then you need to modify the second argument of open.
I'm creating a script that reads the file lines into an array and then searches and print the words that have 2 consecutive vowels in them
Neither your script nor my refactoring of it are doing exactly this. They're both printing whole lines on which there are two consecutive vowels anywhere on the line. The following script parses each line into words (where "words" are contiguous strings of non-whitespace characters) and then prints each word that has two consecutive vowels in it (where "vowels" are the Latin letters A/a, E/e, I/i, O/o and U/u).
#!/usr/local/bin/perl use strict; use warnings; use open qw( :encoding(UTF-8) :std ); @ARGV or die "Usage: perl $0 file ...\n"; while (my $line = <ARGV>) { chomp $line; my @words = split ' ', $line; for my $word (@words) { print "$word\n" if $word =~ m/[aeiou][aeiou]/i; } } exit 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Vowel search
by Noob@Perl (Novice) on Jun 11, 2014 at 22:20 UTC | |
by Jim (Curate) on Jun 11, 2014 at 22:35 UTC | |
by Noob@Perl (Novice) on Jun 11, 2014 at 22:41 UTC | |
by Jim (Curate) on Jun 11, 2014 at 22:48 UTC | |
by Noob@Perl (Novice) on Jun 11, 2014 at 22:51 UTC |