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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:04:05+00:00 2026-05-31T03:04:05+00:00

I am trying to take an input file with @ARGV array and write it’s

  • 0

I am trying to take an input file with @ARGV array and write it’s all elements to an Array with delimiter space in perl.

sample input parameter is a txt file for example:

0145 2145
4578 47896
45 78841
1249 24873

(there are multiple of lines but here it is not shown)
the problem is:

  1. I do not know how to take for example ARG[0] to an array
  2. I want to take every string of that inputfile as single strings in other words the line1 0145 2145 will not be a string it will be two distinct string by delimiting with space.
  • 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-31T03:04:06+00:00Added an answer on May 31, 2026 at 3:04 am

    I think this is what you want. The @resultarray in this code will end up holding a list of digits.

    If you’re giving your program a file name to use as input from the command line, take it off the ARGV array and open it as a filehandle:

    my $filename = $ARGV[0];
    open(my $filehandle, '<', $filename) or die "Could not open $filename\n";
    

    Once you have the filehandle you can loop over each line of the file using a while loop. chomp takes that new line character off the end of each line. Use split to split each line up based on whitespace. This returns an array (@linearray in my code) containing a list of the numbers within that line. I then push my line array on to the end of my @resultarray.

    my @resultarray;
    while(my $line = <$filehandle>){
        chomp $line;
        my @linearray = split(" ", $line);
        push(@resultarray, @linearray);
    }
    

    And remember to add

    use warnings;
    use strict;
    

    at the top of your perl file to help you if you get any problems in your code.


    Just to clarify how you can deal with different inputs to your program. The following command:

    perlfile.pl < inputfile.txt
    

    Will take the contents of inputfile.txt and pipe it to the STDIN filehandle. You can then use this filehandle to access the contents of inputfile.txt

    while(my $line = <STDIN>){
        # do something with this $line
    }
    

    But you can also give your program a number of file names to read by placing them after the execution command:

    perlfile.pl inputfile1.txt inputfile2.txt
    

    These file names will be read as strings and placed into the @ARGV array so that the array will look like this:

    @ARGV:
        [0] => "inputfile1.txt"
        [1] => "inputfile2.txt"
    

    Since these are just the names of files, you need to open the file in perl before you access the file’s contents. So for inputfile1.txt:

    my $filename1 = shift(@ARGV);
    open(my $filehandle1, '<', $filename1) or die "Can't open $filename1";
    
    while(my $line = <$filehandle1>){
        # do something with this line
    }
    

    Note how I’ve used shift this time to get the next element within the array @ARGV. See the perldoc for more details on shift. And also more information on open.

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

Sidebar

Related Questions

Im trying to create a method that take 2 int array as the input
I'm trying to take this file input, which is a multi-file input. <input name=userfile[]
I am trying to take a CSV file as input and transform it into
So I'm basically just trying to take in some file input, and then take
I am trying to take a rather large CSV file and insert it into
I was trying to make a code that will take input from a user
In my HTML file I have the following to take input in the following
I am trying to take user input and convert it in the form of
I have been trying to take chars from a txt file(in which the words
I'm trying to make a calculator that will take inputs from users and estimate

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.