Tie 2 Dimention Array to File

O

OttawaTrade

here is the default form tie::file
use Tie::File;

tie @array, 'Tie::File', 'data.txt' or die ...;

my data.txt looks like this

ID|number|name|address|
a|a.number|a.name|a.address|
b|b.number|b.name|b.address|

this is basically a read only table.
i hope to be able to sort by name, number or address etc.

so i want to sort
$array[1][2]
$array[2][2]
$array[3][2]

etc

i have done the sort part in a 1D array. so the only problem i have is how
to tie a 2D arry to file.

how do i define @array 2D? how does Tie:File know what the seperator is?
 
A

Anno Siegel

OttawaTrade said:
here is the default form tie::file
use Tie::File;

tie @array, 'Tie::File', 'data.txt' or die ...;

my data.txt looks like this

ID|number|name|address|
a|a.number|a.name|a.address|
b|b.number|b.name|b.address|

this is basically a read only table.
i hope to be able to sort by name, number or address etc.

so i want to sort
$array[1][2]
$array[2][2]
$array[3][2]

etc

i have done the sort part in a 1D array. so the only problem i have is how
to tie a 2D arry to file.

how do i define @array 2D? how does Tie:File know what the seperator is?

You don't. Tie::File ties an array so that its elements are lines of the
file. The lines are strings, nothing else so the result will always
be a one-dimensional array.

The data you have is known as CSV (Comma Separated Values), though the
separator is "|", not ",". A CPAN search for "Tie" and "CSV" yields
Tie::CSV_File, which ought to do what you want.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top