Welcome to the monastery. Please note, as documented in How do I post a question effectively?, you should wrap both your code and input in <code> tags, to prevent the site formatting from mangling your input. For example, I presume there are a large number of newlines that have been removed from your post. I have reformatted your input content as
SLNO FirstName LastName Desgination ----------------------------------------------------- 1 Pragya Singh Developer 2 George Shepered SME 3 Akshay Bhargav SME 4 Amey Deshmukh Developer 5 Bhushan Bhatkar Quality 6 Dinesh Yadav Support ------------------------------------------------------
and your code as
#!/perl/bin/perl $fname = "Designations.txt"; open (WXYZ, $fname) or die "Couldn't open file Designations.txt, $!"; while (<WXYZ>) { print "$_"; } print "Enter First Name or Last Name: \n \n"; $name = <STDIN>; print "Entered name is : $name \n";
It's considered good practice for a number of reasons to use strict, indirect file handles, and three-argument open. Fixing that plus some extraneous minor issues would transform your code to:
#!/perl/bin/perl use strict; use warnings; my $fname = 'Designations.txt'; open (my $fh, '<', $fname) or die "Couldn't open file $fname, $!"; while (<$fh>) { print; } print "Enter First Name or Last Name: \n \n"; my $name = <STDIN>; print "Entered name is : $name \n";
In order to do the mapping you suggest, you should probably build a hash in conjunction with split in your while loop. The fact that you've presupposed using "if statements and match operators" makes me think this is a homework assignment, in which case you should tell us as much. My solution might look like:
#!/perl/bin/perl use strict; use warnings; my $fname = "Designations.txt"; open (my $fh, '<', $fname) or die "Couldn't open file $fname, $!"; my %designation; while (<$fh>) { my ($SLNO, $first, $last, $desig) = split /\s+/; $designation{$first} = $desig; $designation{$last} = $desig; } print "Enter First Name or Last Name: \n \n"; chomp(my $name = <STDIN>); print "Entered name is : $name \n"; print "Designation is : $designation{$name}\n";
Of course, this could fail for a number of potential reasons, but identifying those are a exercise for the user.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: How to search a Name from a file and extracts its designation by kennethk
in thread How to search a Name from a file and extracts its designation by amey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.