Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:13:43+00:00 2026-06-13T06:13:43+00:00

I’ve recently been trying to teach myself Perl and have been doing some basic

  • 0

I’ve recently been trying to teach myself Perl and have been doing some basic exercises. In one of them, you have a hard-coded hash of last names to first names. The user inputs a last name and you output the first name — relatively straightforward. The code is as follows:

#!/usr/bin/perl -w

use strict;
use warnings;

my %first_name = (
    Doe => 'John',
    Johnson => 'Bob',
    Pitt => 'Brad',
);

print "What is your last name?\n";
chomp (my $last_name = <STDIN>);
print "Your first name is $first_name{$last_name}.\n";

Now, something strange happens. The “What is your last name?\n” line is not displayed until I input something to the program (and press Enter), after which, the following is printed:

What is your last name?
Your first name is .
Use of uninitialized value within %first_name in concatenation (.) or string at     test.pl line 14, <STDIN> line 1.

Now, I understand the notion of buffered output and all that, and, if I add $| = 1 at the beginning of my program, it works. However, my expectation would be that even without that line, even though print statement strings might not print immediately, my input string will still be placed in the $last_name variable, which it is not. So, I have two questions regarding this:

  1. Why is this happening? Is it an OS thing (I’m running on Windows)?
  2. Why is it that adding a \n does not flush the output (as various sources say it does)?

Note: If I replace the last line that accesses the %first_name hash with a simple printing of the $last_name variable, then, even though the output is still “delayed”, the variable has the correct value.

Note #2: Alternatively, if the code after printing the last name is replaced with this,

if (exists $first_name{$last_name}){ 
   print "Your first name is $first_name{$last_name}.\n";
}

else{
    print "Last name is not in hash.\n";
}

then $last_name does get assigned the correct value from <STDIN>. I am not sure what to make of this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:13:44+00:00Added an answer on June 13, 2026 at 6:13 am

    You are not checking in your program whether the last name is in the hash or not, if it is not then you should display some message like “$lastname not found”.

    By the way your program is working fine on my side if I enter the correct lastname (which exists in the hash).

    So you may edit your program like this:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %first_name = (
        Doe => 'John',
        Johnson => 'Bob',
        Pitt => 'Brad',
    );
    
    print "What is your last name?\n";
    chomp (my $last_name = <STDIN>);
    
    # Check if the last_name exists in hash or not
    if (exists $first_name{$last_name}){ 
       print "Your first name is $first_name{$last_name}.\n";
    }
    
    # If it doesn't then show a custom message
    else{
        print "not found";
    }    
    

    Perhaps you are suffering from buffering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.