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 6069937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:52:37+00:00 2026-05-23T09:52:37+00:00

I’m very new to Perl. I’ve started this tutorial http://www.comp.leeds.ac.uk/Perl/ . There’s an exercise

  • 0

I’m very new to Perl. I’ve started this tutorial http://www.comp.leeds.ac.uk/Perl/. There’s an exercise in the filehandling section which reads:

Modify the above program so that the
entire file is printed with a # symbol
at the beginning of each line. You
should only have to add one line and
modify another. Use the $” variable.

This is the program:

#!/usr/local/bin/perl
#
# Program to open the password file, read it in,
# print it, and close it again.

$file = '/etc/passwd';      # Name the file
open(INFO, $file);      # Open the file
@lines = <INFO>;        # Read it into an array
close(INFO);            # Close the file
print @lines;           # Print the array

Could someone help me with this very easy task? Also, what does it mean when it mentions the $” variable? Thanks in advance.

  • 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-05-23T09:52:38+00:00Added an answer on May 23, 2026 at 9:52 am

    The key to this is understanding the use of the $" variable (note: this is not the same as the $_ variable). The $" variable:

    This is the separator used between list elements when an array variable
    is interpolated into a double-quoted string. Normally, its value is a space
    character.

    What does this mean? It means that there is a way to convert an array of items into a string context, with each item seperated by a special character. By default, that special character is a space…but we can change what that special character is by changing the $" variable.

    SPOILER ALERT

    The below text contains the solution to the exercise!

    SPOILER ALERT

    So, the first part of this exercise is to print out the file in a string context, instead of an array. Let’s pretend we have a fake file whose contents are:

    [/etc/passwd]
    User1
    User2
    User3
    
    [exercise.pl]
    #!/usr/local/bin/perl   
    #   
    # Program to open the password file, read it in,   
    # print it, and close it again.      
    $file = '/etc/passwd';      # Name the file   
    open(INFO, $file);          # Open the file   
    @lines = <INFO>;            # Read it into an array   
    close(INFO);                # Close the file   
    print "@lines";             # Print the array   <---- Notice the double quotes
    
    [RESULT]
    User1
     User2
     User3
    

    Notice that space added in between the elements? That’s because when we interpolate the array into a string context, the $" variable comes into play, and adds a space in between each element as it is concatenated. What we need to do next is change that space into a “#”. We can change the $" variable before printing to do this:

    [exercise.pl]
    #!/usr/local/bin/perl   
    #   
    # Program to open the password file, read it in,   
    # print it, and close it again.      
    $file = '/etc/passwd';      # Name the file   
    open(INFO, $file);          # Open the file   
    @lines = <INFO>;            # Read it into an array   
    close(INFO);                # Close the file
    $" = "#";                   # Change $"         <---- This line has been added!
    print "@lines";             # Print the array   <---- Notice the double quotes
    
    [RESULT]
    User1
    #User2
    #User3
    

    Allright! We’re almost there. The last bit is to get a “#” in front of the very first line. Because $" changes the seperator between elements, it doesn’t affect the very first line! We can finish this off by changing the print statement to print a “#” followed by the contents of the file:

    [exercise.pl]
    #!/usr/local/bin/perl   
    #   
    # Program to open the password file, read it in,   
    # print it, and close it again.      
    $file = '/etc/passwd';      # Name the file   
    open(INFO, $file);          # Open the file   
    @lines = <INFO>;            # Read it into an array   
    close(INFO);                # Close the file
    $" = "#";                   # Change $"         <---- This line has been added!
    print "#" . "@lines";       # Print the array   <---- Notice the double quotes
    
    [RESULT]
    #User1
    #User2
    #User3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6

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.