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!

Move hash into array element 2

Status
Not open for further replies.

rcsen

Programmer
Jan 11, 2001
51
US
Hi,

I am trying to move a "Hash Array" as an element of an Array. I am doing this in a loop. Whenever I try to retrieve the values, I am able to get the last value of the array only.

Is there any special restrictions to do this???

I have tried with
(Loop)
{
$array[0] = \%hash;
}

%hash1 = %{$array[0]};

Also tried
push @array, \%hash;

Can this be done in anyother way???

Thanks for your help in advance.

Sincerely,
RCSEN.
 
if the first element of the array is being set to a new hash every time the loop iterates, the last thing it will be set to is the last element of the iteration. you may want to be doing something more like:[tt]
(loop)
{
$array[0]->{$key} = $value;
}
%hash1 = %{$array[0]};
[/tt]
provided that the $key will be different every time, this will create a hash with one element for every iteration. your syntax otherwise is fine - i'd look to the loop for the problem. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
I'd bet on a loop problem. The syntax you're using to set an array element to a hash reference is correct.

You may also be dereferencing the hash reference improperly when you pull it out of the array. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi,

Thanks for your response.

I have attached the code and the XML file below.


Thanks for your help in advance.

Sincerely,
RCSEN.




#!/usr/local/bin/perl
use XML::DOM;
use CGI;


print "Content-type:text/html\n\n";

my $parser = new XML::DOM::parser;
my $xml_dom = $parser->parsefile('sample.xml');
my $quote_node;
@code = ("^IXIC","ADV","BHP");
@arr="";
%hasharr;

@Tag = ("COMPANY_NAME","LAST","","HIGH","LOW","OPEN","CLOSE","NET_CHANGE","VOLUME","BID","ASK","ERROR_MSG");


for ($sc=0;$sc<=$#code;$sc++)
{
my @quotes = $xml_dom->getElementsByTagName(&quot;QUOTE&quot;);
foreach $quote (@quotes)
{
if ($quote->getAttribute(&quot;SYMBOL&quot;) eq $code[$sc])
{
print &quot;$code[$sc] \n&quot;;
$hasharr{CODE} = $code[$sc];
$quote_node = $quote;
}
if ($quote_node)
{
&parse_xml($quote_node,\%hasharr);
}
$arr[$#arr+1] = \%hasharr;
}

$code1 = $code[$sc];
%parse = %{$arr[$sc]};
&print_template($code1,\%parse);
}


sub parse_xml
{
my ($node,$hasharr) = @_;

if ($node->getNodeType==ELEMENT_NODE)
{
foreach my $child ($node->getChildNodes())
{
&parse_xml($child,$hasharr);
}
}
elsif($node->getNodeType==TEXT_NODE)
{
my ($ParentNode)=$node->getParentNode;
$hasharr{$ParentNode->getTagName} = $node->getData;
}
}


sub print_template
{
my ($stock_code,$parsedData) = @_;
print <<_FROM_;
<html><body>
<center><TABLE border=1>
_FROM_

for ($k=0;$k<=$#Tag;$k++)
{
if ($k==0)
{
print &quot;<TR><font id=p3><B>&quot;.$stock_code.&quot;</B>:&quot;.$parsedData{$Tag[$k]}.&quot;</TR>&quot;;
}
elsif ($k!=$#Tag)
{
print <<_BLOCK_;
<TR>
<TD>
<B>$Tag[$k]</B>
</TD>
<TD align=right width=100>
$parsedData{$Tag[$k++]}
</TD>
<TD align=right width=120><table width=&quot;60%&quot;><tr><td align=left>
<B>$Tag[$k]</B></td></tr></table>
</TD>
<TD align=right width=100>
$parsedData{$Tag[$k]}
</TD>
</TR>
_BLOCK_
}
}
print <<_FROM_;

<BR></TABLE></center></body></html>
_FROM_

}








XML FILE:

<?xml version=&quot;1.0&quot; standalone=&quot;no&quot; ?>
<QUOTE_DATA>
<QUOTE SYMBOL=&quot;^IXIC&quot;>
<COMPANY_NAME>
<![CDATA[ NAS/NMS COMPSITE]]>
</COMPANY_NAME>
<LAST>
<![CDATA[ 1840.29]]>
</LAST>
<NET_CHANGE>
<![CDATA[ 19.72 ]]>
</NET_CHANGE>
<MOVEMENT>N/A</MOVEMENT>
<HIGH>
<![CDATA[ 1855.58 ]]>
</HIGH>
<LOW>
<![CDATA[ 1794.30 ]]>
</LOW>
<OPEN>
<![CDATA[ 1830.42 ]]>
</OPEN>
<CLOSE>
<![CDATA[ ]]>
</CLOSE>
<VOLUME>N/A</VOLUME>
<BID>
<![CDATA[ N/A ]]>
</BID>
<ASK>
<![CDATA[ N/A ]]>
</ASK>
<WEEK_52_LOW>
<![CDATA[ 1794.21 ]]>
</WEEK_52_LOW>
<CURRENCY />
<PE_RATIO>N/A</PE_RATIO>
<MARKET_CAP />
<YIELD>N/A</YIELD>
<BOND_YIELD />
<YEAR_1_RETURN />
<EARNINGS_PER_SHARE>N/A</EARNINGS_PER_SHARE>
</QUOTE>


<QUOTE SYMBOL=&quot;ADV&quot;>
<COMPANY_NAME>
<![CDATA[ ADVANTAGE GROUP ]]>
</COMPANY_NAME>
<LAST>
<![CDATA[ 0.85 ]]>
</LAST>
<NET_CHANGE>
<![CDATA[ -0.06 ]]>
</NET_CHANGE>
<MOVEMENT>N/A</MOVEMENT>
<HIGH>
<![CDATA[ 0.90 ]]>
</HIGH>
<LOW>
<![CDATA[ 0.83 ]]>
</LOW>
<OPEN>
<![CDATA[ 0.90 ]]>
</OPEN>
<CLOSE>
<![CDATA[ ]]>
</CLOSE>
<VOLUME>214,877</VOLUME>
<BID>
<![CDATA[ 0.85 ]]>
</BID>
<ASK>
<![CDATA[ 0.89 ]]>
</ASK>
<WEEK_52_LOW>
<![CDATA[ 0.90 ]]>
</WEEK_52_LOW>
<CURRENCY />
<PE_RATIO>18.570</PE_RATIO>
<MARKET_CAP />
<YIELD>7.140</YIELD>
<BOND_YIELD />
<YEAR_1_RETURN />
<EARNINGS_PER_SHARE>0.049</EARNINGS_PER_SHARE>
</QUOTE>
</QUOTE_DATA>
 
this is the code in question, right?[tt]
for ($sc=0;$sc<=$#code;$sc++)
{
my @quotes = $xml_dom->getElementsByTagName(&quot;QUOTE&quot;);
foreach $quote (@quotes)
{
if ($quote->getAttribute(&quot;SYMBOL&quot;) eq $code[$sc])
{
print &quot;$code[$sc] \n&quot;;
$hasharr{CODE} = $code[$sc];
$quote_node = $quote;
}
if ($quote_node)
{
&parse_xml($quote_node,\%hasharr);
}

$arr[$#arr+1] = \%hasharr;
}

$code1 = $code[$sc];
%parse = %{$arr[$sc]};
...[/tt]

it's putting a new hash reference into the array for every entry in @quote, and each of those hashes is going to have one key, 'CODE'. plus, since the keys are all being named 'CODE', you couldn't put them in the same hash together - they'd overwrite each other. you need to step through you're logic here: what do you want %parse to be filled with? will it be a hash with keys that correspond to the quotes and values that correspond to the code? it looks like you just it to be a list of all the codes, but i don't know. once you have exactly what you need clearly defined, work on the loop structure. say something like:
for every quote,
the hash will have an entry
which will be keyed by the quote (or whatever),
and will have the value of that quote's code.
then this hash will be put onto an array,
to be used later.
then convert this flow into a program structure. remember, an array is a list of values accessible sequentially, a hash is a bunch of values accessible by keys.

sorry i can't help any more, i'm just not sure exactly what you want (and yes, i might be able to gleam more from reading through ALL of the code, but i figure you already know what you're trying to do much better than i do) &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Hi Stillflame,

I'll be having a set of XML tags between <QUOTE> </QUOTE>.

I wanted to parse all the tags between them and store it in a HASH.

Then, if I have another <QUOTE> tag , I should parse that aswell. So, I would like to move the previous hash value somewhere and populate it with new values.

I might have 10 to 15 Quote tags,thats why I thought of moving the hash to an array.

But I got struck with it and unable to proceed.

I have to retrieve the same values from all the QUOTE tags.


If you have time,please let me know where to make changes in parsing and displaying.

Thanks
RCSEN
 
okay then, exactly what will the hash have as keys and values? determine this, and do exactly that in the loop, using syntax something like this:[tt]
$hash{$key} = $value;
...
}
push @array, \%hash;
[/tt]
push the hash onto the array only after it's been completely populated, or dereference on the fly(like i hinted in an earlier post), but either way, you should only put one hash on for every complete @quote array, not every entry in the @quote array. also remember that when you fill a hash, any duplication of keys will overwrite the old key, so make your keys unique. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Hi,

Is there anyway to generate HASH names dynamically???
So that I could move every QUOTE tag in the XML file to a different HASH.

Thanks for your help.
RCSEN

 
Sure, try this:
Code:
$hashname = &quot;tsdragon&quot;;
${$hashname}{'fname'} = &quot;Tracy&quot;;
${$hashname}{'lname'} = &quot;Dryden&quot;;
This will generate a hash named %tsdragon!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks Stillflame and Tracy for spending your precious time.

Now, I am genetrating a HASH dynamically for EVERY QUOTE.

Its working fine now.

Thanks.
-RCSEN
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top