2010

Perl read a file and an array and find common words

Question:

This is kind of a small issue and I hope you are able to help me. My code is probably rubbish. For an example, I have a file in which the only statement is John is the uncle of Sam. My Perl script should <strong>copy</strong> the file contents into an array. User should be able to input different names and search if those names are mentioned in the file. There should be an array with relationships like "uncle aunt, mother, father etc" in the program.

#use warnings; use Array::Utils qw(:all); print "Please enter the name of the file\n"; my $c = <STDIN>; open(NEW,$c) or die "The file cannot be opened"; @d = <NEW>; print @d, "\n"; @g = qw(aunt uncle father); chomp @d; chomp @g; my $e; my $f; print "Please enter the name of the first person\n"; my $a = <STDIN>; print "Please enter the name of the second person\n"; my $b = <STDIN>; my @isect = intersect(@g, @d); print @isect; foreach(@d) { if ($a == $_) { $e = $a; } else { print "The first person is not mentioned in the article"; exit(); } if ($b == $_) { $f = $b; } else { print "The second person is not mentioned in the article"; exit(); } } print $e; print $f; close(NEW);

This is something that I have done so far, the intersection is not giving the word uncle which is the word common in both arrays. The program is taking any random name and printing them. It is not saying that the name doesn't exist in the file when I enter a different name other than John and Sam

Answer1:

There are several problems:

<ol><li>

You do not chomp $c. The filename contains a newline at the end.

</li> <li>

You use the 2-argument form of open, but do not test the second argument. This is a security problem: do you know what happens if the user input contains > or |?

</li> <li>

You use == to compare strings. String equality is tested with eq, though, == tests numbers.

</li> <li>

Moreover, you do not want to know whether "Sam" equals to "John is the uncle of Sam". You want to know whether it is a part of it. You might need to use index or regular expressions to find out.

</li> <li>

Do not use $a as the name of a variable, it is special (see <a href="http://p3rl.org/perlvar" rel="nofollow">perlvar</a>).

</li> </ol>

Answer2:

Do not try to compare strings with ==! Use eq (equals) instead. Also you didnt chomp your input $a$b`. I think this is what you're trying to do:

#!/usr/bin/perl use strict; use warnings; print "Please enter the name of the file\n"; my $c = <STDIN>; open(NEW,$c) or die "The file cannot be opened"; my @d = <NEW>; chomp @d; my $e; my $f; print "Please enter the name of the first person\n"; my $aa = <STDIN>; print "Please enter the name of the second person\n"; my $bb = <STDIN>; chomp $aa; chomp $bb; my $pattern_a = quotemeta $aa; my $pattern_b = quotemeta $bb; foreach (@d){ if ($_ =~ /$pattern_a/){ $e = $aa; } elsif ($_ =~ /$pattern_b/){ $f = $bb; } } close(NEW); unless ($e){ print "First person not mentionend\n"; } unless ($f){ print "Second person not mentioned\n"; }

Recommend

  • FQL query to get the Immediate family (name, birthday and relationship)
  • Jquery Date picker can't saving in database by php
  • Re-render navigation bar after login on vuejs
  • Implementing pipe using shared memory
  • Another rule as argument to a rule in prolog
  • Rvest: getting node text and not its childen's text
  • Use of eval to load modules
  • Perl - Using regex to match input in hash key or value
  • How do I split up a line and rearrange its elements?
  • List or scalar context when returning an array or list with a single element
  • Removing multiple recurring text from pandas rows`
  • Using Perl, how do I check if a process with given name is running or not?
  • Openshift node app failed to start
  • import error with python-mysql-connector 1.16, django 1.6, and python 3.2.3
  • Django Class view didn't return an HttpResponse object. It returned None instead
  • Read stdin in chunks in Bash pipe
  • System call time out?
  • How can I count unique terms in a plaintext file case-insensitively?
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Interpreting STRACE output - pipes and forks
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Memory error in python- how to use more memory
  • Differences in dis-assembled C code of GCC and Borland?
  • Algorithm for a smudge tool?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal