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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:28:18+00:00 2026-05-28T07:28:18+00:00

I have a data file, with each line having one number, like 10 20

  • 0

I have a data file, with each line having one number, like

10
20
30
40

How do I read this file and store the data into an array?

So that I can conduct some operations on this array.

  • 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-28T07:28:19+00:00Added an answer on May 28, 2026 at 7:28 am

    Just reading the file into an array, one line per element, is trivial:

    open my $handle, '<', $path_to_file;
    chomp(my @lines = <$handle>);
    close $handle;
    

    Now the lines of the file are in the array @lines.

    If you want to make sure there is error handling for open and close, do something like this (in the snipped below, we open the file in UTF-8 mode, too):

    my $handle;
    unless (open $handle, "<:encoding(utf8)", $path_to_file) {
       print STDERR "Could not open file '$path_to_file': $!\n";
       # we return 'undefined', we could also 'die' or 'croak'
       return undef
    }
    chomp(my @lines = <$handle>);
    unless (close $handle) {
       # what does it mean if close yields an error and you are just reading?
       print STDERR "Don't care error while closing '$path_to_file': $!\n";
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file, with each line holding one number. I am trying
I have binary data in a file that I can read into a byte
I have a data file that looks like the following example. I've added '%'
I need to read a line like this from a file: 5 Chair 12.49
I have a file log that I would like to parse and am having
I am using a text file that stores 3 columns of data, each having
I have a file wich has about 12 millon lines, each line looks like
Say I have a data file that I want to process; I want to
I have a CSV data file with rows that may have lots of columns
I have a file data.base which looks like: 1234 XXXX 4321 XXXX 9884 ZZZZ

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.