diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-15 00:32:53 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-15 00:32:53 +0100 |
| commit | 89246f6d38156a805a660e8cc03d7c312b16328d (patch) | |
| tree | 6344356ecaca54772a217603620cd94e923ded50 /challenge-064 | |
| parent | 7f736f61354f3fa72e8a87a124caa194281c2f2a (diff) | |
| download | perlweeklychallenge-club-89246f6d38156a805a660e8cc03d7c312b16328d.tar.gz perlweeklychallenge-club-89246f6d38156a805a660e8cc03d7c312b16328d.tar.bz2 perlweeklychallenge-club-89246f6d38156a805a660e8cc03d7c312b16328d.zip | |
- Added solution by Stephanie Stein.
Diffstat (limited to 'challenge-064')
| -rw-r--r-- | challenge-064/stephanie-stein/perl/ch-2.pl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-064/stephanie-stein/perl/ch-2.pl b/challenge-064/stephanie-stein/perl/ch-2.pl new file mode 100644 index 0000000000..48a852ddea --- /dev/null +++ b/challenge-064/stephanie-stein/perl/ch-2.pl @@ -0,0 +1,57 @@ +use strict;
+use warnings;
+use List::MoreUtils qw(uniq);
+
+
+
+sub in_string{
+
+
+
+#taking user input
+#this is based on https://www.codesdope.com/perl-input/
+ print "First, enter the sentence or string you'd like to search in:\n ";
+ my $S=<>; #if you'd rather enter the string in the code, put it here
+ chomp($S);
+ print "Next, type each word you want to look for. Hit Enter to type the next word. \nWhen you've finished entering all of the words, hit CTRL-Z for Windows or CTRL-D for Linux\n";
+ my @w_list=<>; #if you'd rather enter the list in the code, put it here
+ chomp(@w_list);
+
+
+#this syntax is based on https://www.learn-perl.org/en/Subroutines
+ my $W=\@w_list;
+ my @answer=();
+ my %place;
+
+ my @W=@$W;
+ my @UW= uniq@W;
+
+ foreach(@UW){
+ $_=lc($_); #trying to make it so it's not case-sensiive
+
+ #storing the order
+ $place{$_}=index($S,$_) if(lc($S)=~/$_/);
+ #print "this is place",%place;
+}
+
+if ((keys %place)<1){
+ return 0;
+}else{
+ #soring substrings based on their place in the string
+ #based on https://perlmaven.com/how-to-sort-a-hash-in-perl
+ # foreach my $subs(sort { $place{$a} <=> $place{$b} } keys %place){
+
+foreach my $name (sort {$place{$a} <=>
+ $place{$b}} keys %place) {
+ push (@answer,$name);
+
+
+}
+return @answer;
+}
+
+
+}
+
+my @show_ans=in_string();
+print "this is the answer: ", @show_ans, "\n"
|
