Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Sounds like to need to look at perldoc perllol and perldoc perdsc, both of which cover this in some detail.

In summary you'd do something like this (assuming your delimiter is a tab):

my @data; open(DATA, $file) || die "Can't open $file: $!\n"; while (<DATA>) { push @data, [split /\t/]; }

You would then access the various elements like this:

my $town = 'London'; my ($lat, $long); foreach (@data) { if $_->[0] eq $town; ($lat, $long) = ($_->[1], $_->[2]); last; }

However, if this is how you are going to be using the data, then you might be better off building a hash of arrays, where the key to the hash is the town name and the value is a two element list containing lat and long. You would construct that something like this:

my %data; open(DATA, $file) || die "Can't open $file: $!\n"; while (<DATA>) { my ($town, @vals) = split(/\t/); $data{$town} = \@vals; }

you could then get the lat and long for a particular town like this:

my ($lat, $long) = @{$data{$town}};

Hope this helps

--
<http://www.dave.org.uk>

European Perl Conference - Sept 22/24 2000
<http://www.yapc.org/Europe/>

In reply to Re: Reading file into an array and working with it. by davorg
in thread Reading file into an array and working with it. by Speedfreak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found