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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:13:16+00:00 2026-05-16T00:13:16+00:00

I am just trying to learn some Perl, so I am going through one

  • 0

I am just trying to learn some Perl, so I am going through one function just to get grip on the language. Could somebody explain to me what this function is doing exactly?

#! /usr/bin/perl 
use strict; 

my %hash; 

&Parse('first.txt'); 
&Parse('second.txt'); 

my $outputpath = 'output.txt'; 
unlink ($outputpath); 
open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!"; 
print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash); 
close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!"; 

sub Parse { 
    my $inputpath = shift; 
    open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!"; 
    while (<INPUT>) { 
        chomp; 
        my @row = split(/\t/, $_); 
        my $col1 = $row[0]; 
        shift @row; 
        push(@{$hash{$col1}}, @row); 
    } 
    close (INPUT) || die "Failed to close INPUT ($inputpath) - $!"; 
    return 1; 
}

I am more interested in shift and push and chomp.

  • 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-16T00:13:16+00:00Added an answer on May 16, 2026 at 12:13 am

    Edit: You posted some extra code, I’ll comment that as well.

    #!/usr/bin/perl 
    #The first line (has to be first, hence this comment comes after) allows the linux shell to know 
    #this is a perl program, and to call perl to execute it.
    
    #use strict: allow stricter checking of perl syntax. You should always do this.
    use strict; 
    
    #declare a global variable called hash - not a very good name...
    my %hash; 
    
    #call the method with 'first.txt' as argument
    &Parse('first.txt'); 
    &Parse('second.txt');  #same thing, different parameter
    
    
    my $outputpath = 'output.txt'; 
    
    #destroy the file declared above if it exists
    unlink ($outputpath); 
    
    # open the file for append (could simple have opened for output and not used the unlink above...)
    open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!"; 
    
    #print a line to output 
    #the line comes from a foreach loop
    #the foreach loop runs over the hash, sorted by key
    #each hash entry contains an array, this array is converted by a string using the JOIN function
    # the join function will paste the elements of the array into a string, seperated by a tab
    print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash); 
    
    #Close the outputfile
    close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!"; 
    

    This program was probably written some time ago – Modern Perl looks a bit different and has a few best practices that are not here yet.

    Don’t use this as an example on how to write Perl. Maybe Ether will rewrite this for you, if you smile nicely 🙂

    #declare a sub
    sub Parse { 
        # the parameters given to a sub are stored in @_. 
        #shift without arguments takes the first element from @_
        my $inputpath = shift; 
        #opens the file "inputpath" into fileglob INPUT. 
        #If this fails, quit with an error message
        open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!"; 
    
        #loop over the file one line at the time, putting each line in $_
        while (<INPUT>) { 
    
            #chop = remove last character. chomp = remove last character if it is a CRLF. 
            #Without arguments, works on $_ 
            chomp; 
    
            #split the $_ variable (containing the row) 
            #into an array based on the tab character
            my @row = split(/\t/, $_); 
    
            # take the first element into col1
            my $col1 = $row[0]; 
    
            # shift it (remove the first element)
            shift @row; 
    
            # actually both rows above can be just written as one statement:
            my $col1 = shift @row;
    
            #the $hash variable is probably a global hashref defined somewhere above...
            #the $hash hashref now contains a bucket named with the 'col1' value
            # the value of that bucket is the array of the row we just read
            push(@{$hash{$col1}}, @row); 
    
            # end the while loop
        } 
    
        #close the file or die
        close (INPUT) || die "Failed to close INPUT ($inputpath) - $!"; 
    
        #end the method
        return 1; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just trying to learn some Lisp, so I am going through project
I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database
For the past few weeks, I've been trying to learn about just how email
im just starting to learn flex and im trying to understand how Flex does
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my irb sessions to actually list the current line of
Just trying to get up to speed with the SDK... So, I've created my
Just trying to get my head round Spring and figuring out how I wire
Developing a website and just trying to get back into the swing of (clever)
I'm just trying to get a general idea of what views are used for

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.