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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting values into a hash

Status
Not open for further replies.

MariusX1

Programmer
Dec 9, 2003
10
US
Just a quick question. I know this can be done with more lines, but I'm wondering if it's possible to make the following work in one line.

(values(%my_hash)) = split(/\|/,$line);

I was hoping this would work since you can do the same thing with a regular array, but it's not working as is. Any ideas on an elegant solution? Thanks!

Doug
 
Hi doug,

should work....

%hash = split() ?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I think I realized an error in my logic actually. What I'm really trying to do is read in a delimited file. I want the header row to become the keys of the hash array. Subsequent lines will become the values. I thought it would work like this:

if ($line_count eq 1)
{
(keys(%my_hash)) = split(/\|/,$line);
}
else
{
(values(%my_hash)) = split(/\|/,$line);
}

Basically, for a given line, I want the values to hold the data in that line while the keys will always hold the header values. Basically like creating columns out of a hash array.

I think the problem is that a hash won't necessarily put the fields in the same order every time. So, some values might get associated with the wrong keys.

I know I could use an array of arrays, but that seems overly complex for what I'm trying to do. IN THEORY, a hash would work great in this situation. Any more ideas?

It seems like doing %hash = split() wouldn't split out the keys and values the way that I want them. Thanks!!

Doug
 
I think I've done what you were thinking about here. The way I did it was to store the column headers in an array when I read the first line of the file, then use the array to control the order when I ouput the values in each hash. That way you don't have a problem with columns printing out
in the wrong order. Here's some code.
Code:
#!perl

use strict;

my @headers;
my %hash;
my @arr;

while (<>)
{
	chomp;
	if ($. == 1)
	{
		@headers = split /\t/;
		next;
	}
	@hash{@headers} = split /\s+/;
	push @arr, { %hash } ;
}


for my $i (0..$#arr)
{
	if ($i == 0)
	{
		print join(&quot;\t&quot;, @headers), &quot;\n&quot;
	}

	for my $h (@headers)
	{
		print qq($arr[$i]->{$h}\t);
	}
	print qq(\n);
}

 
In fact, the &quot;for my $h&quot; loop could be done away with and the loop that does the printing written like this
Code:
for my $i (0..$#arr)
{
	if ($i == 0)
	{
		print join(&quot;\t&quot;, @headers), &quot;\n&quot;
	}
	print join(&quot;\t&quot;, @{$arr[$i]}{@headers}), &quot;\n&quot;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top