Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a file as a hash 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
How do I open a csv text file as a hash. I know this much:

open ( TABLE, table1 ) or die ("cannot open file : $!" );

Now I want it written into a hash in this manner:

Contents of the csv file:

"some HEX number", "text associated with some HEX number", "next HEX number", "text associated with next HEX number", etc, etc,

I want the HEX number column in the HASH to be the key so I can search for it and pull the associated text.

Any ideas?
 
question: are there commas inbetween all the numbers and strings? if so, the task becomes much simplified: read the contents of the file into a string, split that string on commas, and then take that list and make a hash out of it. here's the lookings of that:[tt]
my $file = join('', <TABLE>);
#no space in ^^ between those quotes.
my %file = split(&quot;,&quot;, $file);
[/tt]
if that's not the case, and you'll have to search out each individual number and string with a regular expression in a while loop, something like:[tt]
my $file = join('', <TABLE>);
my %hashFile;
while($file =~ m/(0x\d+)(.*?(?=0x\d+))/) {
$hashFile{$1} = $2;
}
[/tt]
i hope that '?=...' in regex is a zero-width look-ahead, otherwise, replace it with whatever IS a zero-width look-ahead, and that should work. i didn't actually test this, so it may require some twiking(is that a word?).
good luck, i hope some of this helps.
stillflame &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top