2D Array from Text File 
Author Message
 2D Array from Text File

I would like to generate a 2D array from a text file containing 2 lines -
one line containing a 2 digit source code, and the other with corresponding
descriptions for the codes.  This way I can easily access the list of source
codes and their descriptions at will.

What is the best way of doing this?

eg: file as follows:

AA,BB,CC,DD
Alpa Pub,Number Two,Tertiary Print,Forth Chapter

How then would you display a list of source codes (AA,BB,CC,DD) and
descriptions?

PAW



Fri, 14 May 2004 21:07:31 GMT  
 2D Array from Text File

Quote:

> I would like to generate a 2D array from a text file containing 2 lines -
> one line containing a 2 digit source code, and the other with corresponding
> descriptions for the codes.  This way I can easily access the list of source
> codes and their descriptions at will.

> What is the best way of doing this?

> eg: file as follows:

> AA,BB,CC,DD
> Alpa Pub,Number Two,Tertiary Print,Forth Chapter

> How then would you display a list of source codes (AA,BB,CC,DD) and
> descriptions?

use a hash

use split to generate a list of keys from the first line, then use split
again to generate a list of values from the second line...plug them into the
hash with for loops.

--
robert sherman
css3, school of civil and environmental engineering
georgia institute of technology
atlanta, ga, usa



Fri, 14 May 2004 22:34:26 GMT  
 2D Array from Text File
As it was already said , use a hash.
Here's a bit of sample code :

...
my (%hash,$i);



        $hash{$_} = $desc[$i++]

Quote:
}      

...

Now you can access all the descriptions as values of the hash.

Lao



Fri, 14 May 2004 23:07:03 GMT  
 2D Array from Text File

Quote:

> I would like to generate a 2D array from a text file containing 2 lines -
> one line containing a 2 digit source code, and the other with corresponding
> descriptions for the codes.  This way I can easily access the list of source
> codes and their descriptions at will.

> What is the best way of doing this?

> eg: file as follows:

> AA,BB,CC,DD
> Alpa Pub,Number Two,Tertiary Print,Forth Chapter

> How then would you display a list of source codes (AA,BB,CC,DD) and
> descriptions?

> PAW


print "$_=>$h{$_}\n" for sort keys %h;
__END__
AA,BB,CC,DD
Alpa Pub,Number Two,Tertiary Print,Forth Chapter

--
Bob Walton



Sat, 15 May 2004 03:33:21 GMT  
 2D Array from Text File
Thanks for your posts!

In rethinking my set-up to this and wonder if it would be better to have a
text file with 'n' number of lines.  Each line would start with my code
followed by a comma and then the description:

AA,Alpha Description
BB,Beta Description

etc.

How would setting up the hash differ for this scenario?

PAW



Sat, 15 May 2004 21:17:16 GMT  
 2D Array from Text File


| Thanks for your posts!
|
| In rethinking my set-up to this and wonder if it would be better to have a
| text file with 'n' number of lines.  Each line would start with my code
| followed by a comma and then the description:
|
| AA,Alpha Description
| BB,Beta Description
|
| etc.
|
| How would setting up the hash differ for this scenario?

open FH... # do that yourself.

my %hash;
while (<FH>) {
   chomp;
   my ($key, $value) = split /,/ $_;
   $hash{$key} = $value;

Quote:
}

Steffen
--
$_=q;0cb212c210b0bb010c0113bb0c410c0b516c0bb3d212c2b0b0b016b6cb2b2c21010c0
b41110b3bba0e0c0d2c4b2b6bc013d2c0d0b01012b0b0;;s/\n//g;s/(\d)/$1<2?$1:'0'x




Sat, 15 May 2004 21:26:41 GMT  
 2D Array from Text File

Quote:

>Thanks for your posts!

>In rethinking my set-up to this and wonder if it would be better to have a
>text file with 'n' number of lines.  Each line would start with my code
>followed by a comma and then the description:

>AA,Alpha Description
>BB,Beta Description

>etc.

>How would setting up the hash differ for this scenario?

It'd look like this:

        my ($line, $key, $value);
        while ($line = <THEFILE>)
        {
            chomp $line;
            ($key, $value) = split (/,/, $line);
            $hash{$key} = $value;
        }

Or if you like to use implicit variables:

        while (<THEFILE>)
        {
            chomp;
            /(.*),(.*)/;
            $hash{$1} = $2;
        }

Hope that helps.

  - Logan
--
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos



Sat, 15 May 2004 21:30:59 GMT  
 2D Array from Text File

Quote:




>            $hash{$_} = $desc[$i++]
> }  

Or, eliminating the 'for' loop and using a hash slice:


BTW - I don't like naming a variable for its type.  I'd rather name it
for the contents.  Some folks don't like that there's a hash and an
array both named 'desc' in the following code.  I can hear that as a
valid argument.  I kinda' like having the hash and array name be the
same -- it "says" something to me.  It's documentation to me.  If it's
confusion to you, choose a different.  Style.



# The hash 'desc' is initialized from the array 'desc'.

--
All mammals learn by playing.



Sun, 16 May 2004 15:19:25 GMT  
 2D Array from Text File

Quote:

> AA,Alpha Description
> BB,Beta Description

Much better file structure.  Much better to maintain.  Yeah!!!!

Just for kicks, you could implement "comments" in the file to help
future maintainers.  I often write loops that throw out comment and
blank lines.  The following snippet throws out "shell-style" comments
(i.e. sharp as the first non-whitespace character on a line) and blank
lines.  I then put a comment at the beginning of the file that looks
like a table header line.  I can then 'group' my data into
'paragraphs', and even introduce each separate paragraph.  I can even
"disable" a line temporarily by "commenting" it out, and let the file
remember what the data is for re-enabling it.

This is one of my favorite perl code frags:

while (<DATA>) {
  next if /^\s*#/ || /^\s*$/;
  # continue with real data

Quote:
}

Alternatively -

while (<DATA>) {
  next if /^\s*#/;      # skip comments
  next if /^\s*$/;      # skip blank lines
  # continue with real data

Quote:
}

__DATA__
# Blah-blah data file:
# The rules for this data file are quite strange!
#/----- Blah-blah code (must be 2 digit prime number)
#|  /-- Blah-blah desc (must not contain vowels or the letter 'M')
#|  |
#V  V

# ================
# From North America
AA,Alpha Description
BB,Beta Description

# ================
# From Central America
CC,asdf
DD,asdf

# ================
# From South America
XY,asdf

# ================
# From Oz
zz,asfd

--
All mammals learn by playing.



Sun, 16 May 2004 15:33:13 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Can't get 2D slices of 2D array of unknown size to work

2. File Parsing & 2D Arrays

3. sorting 1st element on 2d array list

4. 2d arrays

5. 2d arrays

6. How to best refer to sections of 2D arrays

7. Printing a 2d array

8. Passing 2D arrays?

9. # of elements in 2D array

10. How do I sort a 2D-array by three different columns with perl

11. sort 2d array numerically by row?

12. Upper boundries of 2D arrays

 

 
Powered by phpBB® Forum Software