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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:52:40+00:00 2026-05-24T05:52:40+00:00

I’ve been trying to write a program to read columns of text-formatted numbers into

  • 0

I’ve been trying to write a program to read columns of text-formatted numbers into Perl variables.

Basically, I have a file with descriptions and numbers:

ref   5.25676      0.526231      6.325135
ref   1.76234     12.62341       9.1612345

etc.

I’d like to put the numbers into variables with different names, e.g.

ref_1_x=5.25676
ref_1_y=0.526231

etc.

Here’s what I’ve got so far:

print "Loading file ...";
open (FILE, "somefile.txt");
@text=<FILE>;
close FILE;
print "Done!\n";
my $count=0;
foreach $line (@text){
    @coord[$count]=split(/ +/, $line);
}

I’m trying to compare the positions written in the file to each other, so will need another loop after 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-05-24T05:52:40+00:00Added an answer on May 24, 2026 at 5:52 am

    Sorry, you weren’t terribly clear on what you’re trying to do and what “ref” refers to. If I misunderstood your problem please commend and clarify.


    First of all, I would strongly recommend against using variable names to structure data (e.g. using $ref_1_x to store x coordinate for the first row with label “ref”).

    If you want to store x, y and z coordinates, you can do so as an array of 3 elements, pretty much like you did – the only difference is that you want to store an array reference (you can’t store an array as a value in another array in Perl):

    my ($first_column, @data) = split(/ +/, $line); # Remove first "ref" column
    @coordinates[$count++] = \@data; # Store the reference to coordinate array
    

    Then, to access the x coordinate for row 2, you do:

    $coordinates[1]->[0]; # index 1 for row 2; then sub-index 0 for x coordinate.
    

    If you insist on storing the 3 coordinates in named data structure, because sub-index 0 for x coordinate looks less readable – which is a valid concern in general but not really an issue with 3 columns – use a hash instead of array:

    my ($first_column, @data) = split(/ +/, $line); # Remove first "ref" column
    @coordinates[$count++] = { x => $data[0], y => $data[1], z => $data[2] };
    # curly braces - {} - to store hash reference again
    

    Then, to access the x coordinate for row 2, you do:

    $coordinates[1]->{x}; # index 1 for row 2
    

    Now, if you ALSO want to store the rows that have a first column value “ref” in a separate “ref”-labelled data structure, you can do that by wrapping the original @coordinates array into being a value in a hash with a key of “ref”.

    my ($label, @data) = split(/ +/, $line); # Save first "ref" label
    $coordinates{$label} ||= []; # Assign an empty array ref 
                              #if we did not create the array for a given label yet.
    push @{ $coordinates{$label} }, { x => $data[0], y => $data[1], z => $data[2] };
    # Since we don't want to bother counting per individual label, 
    # Simply push the coordinate hash at the end of appropriate array.
    # Since coordinate array is stored as an array reference, 
    # we must dereference for push() to work using @{ MY_ARRAY_REF } syntax
    

    Then, to access the x coordinate for row 2 for label “ref”, you do:

    $label = "ref";
    $coordinates{$label}->[1]->{x}; # index 1 for row 2 for $label
    

    Also, your original example code has a couple of outdated idioms that you may want to write in a better style (use 3-argument form of open(), check for errors on IO operations like open(); use of lexical filehandles; storing entire file in a big array instead of reading line by line).

    Here’s a slightly modified version:

    use strict;
    my %coordinates;
    print "Loading file ...";
    open (my $file, "<", "somefile.txt") || die "Can't read file somefile.txt: $!";
    while (<$file>) {
        chomp;
        my ($label, @data) = split(/ +/); # Splitting $_ where while puts next line
        $coordinates{$label} ||= []; # Assign empty array ref if not yet assigned
        push @{ $coordinates{$label} }
           , { x => $data[0], y => $data[1], z => $data[2] };
    }
    close($file);
    print "Done!\n";
    

    It is not clear what you want to compare to what, so can’t advise on that without further clarifications.

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

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

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.