amey has asked for the wisdom of the Perl Monks concerning the following question:

I am new to PERL so please forgive me If I am being dumb here.

I have a file like below -

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 ------------------------------------------------------

I want to write a PERL program for below -
Provide user an option to print all last names with designation or firstnames with designation. Depending on the user input, print the "lastname designation" or "first name designation". Keep waiting for input if not given.

I gave it a shot but only able to read\open it

#!/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"; --------------------------------------------

After I enter the name it should give me a desgination belonging to that person.

What should I use a while loop along with if statements and match operators ??

Please help me, I am confused what if there are many names in the file.

Please help

Replies are listed 'Best First'.
Re: How to search a Name from a file and extracts its designation
by kennethk (Abbot) on Jan 21, 2015 at 16:02 UTC
    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.

      Thanks Kennethk for re-formatting it for me, I am new here so didn't go through all the notes in the Website. Appreciate the help :)

Re: How to search a Name from a file and extracts its designation
by davido (Cardinal) on Jan 21, 2015 at 17:22 UTC

    You're not thinking (or expressing yourself) carefully enough to get thoughtful answers. You'll get a few good guesses maybe, but correct solutions, probably not (unless someone gets lucky). Here are the unanswered questions we would need to help you with a correct solution:

    • Does your input file really have a header row and a row of hyphens?
    • Does your input file use fixed-width fields, or space-delimited fields? (If fixed-width, why didn't you demonstrate fixed width? If not, you've got a new problem.)
    • If your input file uses space-delimited fields, how does it handle compound names? (Names that consist of more than one word)
    • You say that a first name or a last name can be used as the lookup for a single person in the list. And in the sample data set you provided it appears that would be fine. But in the real world neither first names, nor last names, nor combinations of first and last names are actually unique; you cannot reasonably expect that a name search will result in a single result. So what should happen when there are first name collisions, last name collisions, or first/last collisions?
    • You ask "...what if there are many names in the file." ...yes, what if? How many is many? How big could the file be? How often does the file change? Could we sort it once? Should we be maintaining an index file parallel to the actual data file? We can't answer these questions because we don't know enough about the problem you're solving.

    Each of the solutions you've been provided fail to deal with compound names and name clashes. Neither of them scale particularly well either. But they're both just fine for your limited sample input. This illustrates the point that brief sample input that doesn't show the breadth of possible input, combined with an incomplete description of the problem can lead to answers that seem fine, but will fail in real use.

    If this is a school assignment, go back to the professor and get answers to these questions so that you can more carefully craft a correct solution. If it's a work project, then you should know enough about your client to be able to answer these questions. If you don't, you need to nail down the spec with your client better.

    Only then can you begin to produce a useful solution. And until the details are known that can lead to a useful solution, there's no point in anyone wasting time giving you solutions based on guess work or incomplete specifications, because they will miss some detail that is crucial to the client or to your professor.


    Dave

Re: How to search a Name from a file and extracts its designation
by vinoth.ree (Monsignor) on Jan 21, 2015 at 16:46 UTC
    Hi amey,

    Please find the code below, which meets your requirement.

    #!/perl/bin/perl use strict; use warnings; use Data::Dumper; my $fname = "Designations.txt"; open (WXYZ, $fname) or die "Couldn't open file Designations.txt, $!"; while(1) # This loop is to continously get the user input. { print "Enter First Name or Last Name: \n \n"; my $user_input = <STDIN>; chomp($user_input); if(defined $user_input and length $user_input) { print "Entered name is : $user_input \n"; &find_details($user_input); seek(WXYZ,0,0); } } sub find_details{ my ($uinput) = @_; my @array; while (<WXYZ>) { chomp; next if(/\-+/ || /SLNO/); @array = split /\s+/,$_; my $input; foreach $input (@array[1,2]){ print ">>$input<<\n"; if ($input =~ /$uinput/) { print "$input,$array[3]\n"; return; } } } print ">>>>>>>>>>>>>>>>>>>>>>>>>>>Sorry! User Details not avai +lable<<<<<<<<<<<<<<<<<<<<<<<<<<\n" ; } close(WXYZ);

    1. The first while loop continues runs to get the userinut and calls a find_details function if user enters valid input.

    2. find_detils function reads each line and splits based on space character and saves into array, a foreach inside the function searchs your input against firstname and lastname. If user input matchs, its print the username and designation.

    3.After the function return, I do seek to move the file pointer to move to the begging of the file to make the file ready to search the next input.


    All is well

      Thanks Vinoth.ree for the code, I just went through it and understood most of it. I am Still in the learning phase a long way to go.

Re: How to search a Name from a file and extracts its designation
by LanX (Saint) on Jan 21, 2015 at 16:01 UTC
    welcome! :)

    tip: using <code> tags would make your post readable and attract more answers. .. ;)

    would you mind updating your post?

    Cheers Rolf

    PS: Je suis Charlie!