Ask a Question related to PERL Beginners, Design and Development.
-
Soumyadeep Nandi #1
Updating an array within a hash
Hi Everybdy,
I am stuck in a problem for which I need your help. My
problem spins around adding an element in an array
within a hash.
I have a hash declared as $hash{"1"}=@array; now I
want to add an element to it within a loop. How would
I do this? I would be highly grateful to you if you
could help me.
Regards
Soumyadeep
__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
[url]http://taxes.yahoo.com/filing.html[/url]
Soumyadeep Nandi Guest
-
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
adding an array as a hash value
Hi Dermot. Dermot Paikkos wrote: And use warnings; These variables need to have better names so that it's more -
Array and hash iteration questions
I have a CSV file and I'm trying to do a few things with it. Essentially what it boils down to is: count the number of times a certain value is... -
Array and Hash to_s
On Saturday, July 19, 2003, at 12:56 PM, Yukihiro Matsumoto wrote: While we're on the subject of "to_s" and similar friends, I have a question... -
How to put a filehandle into a hash array?
I need to build an array of filehandles so different records go into different output files depending on a value. This is the code: $dt =... -
Jan Eden #2
Re: Updating an array within a hash
Soumyadeep nandi wrote:
Your syntax puts a number (the number of elements in @array into $hash{"1"}.>Hi Everybdy,
>I am stuck in a problem for which I need your help. My
>problem spins around adding an element in an array
>within a hash.
>I have a hash declared as $hash{"1"}=@array; now I
>want to add an element to it within a loop. How would
>I do this? I would be highly grateful to you if you
>could help me.
I guess you want to set an array reference as the hash key's value:
$hash{"1"} = \@array;
In that case, you can just
push @{$hash{"1"}}, $_ for (...list goes here...);
to push into the array referenced by $hash{"1"}.
Best,
Jan
--
These are my principles and if you don't like them... well, I have others. - Groucho Marx
Jan Eden Guest
-
Stephen Hardisty #3
RE: Updating an array within a hash
> Hi Everybdy,
Hi,> I am stuck in a problem for which I need your help. My
> problem spins around adding an element in an array
> within a hash.
> I have a hash declared as $hash{"1"}=@array; now I
> want to add an element to it within a loop. How would
> I do this? I would be highly grateful to you if you
> could help me.
I'm not sure you can just go "hash = array" as they're somewhat different, in the same way you can't go "scalar = array" (you can but it won't do what you think). But you can assign the hash the reference of the array. For example:
$hash{1} = \@array;
Or:
push(@{$hash{1}}, @array);
Then to loop through the array whose reference is assigned to $hash{1} you can do:
foreach(@{$hash{1}})
{
# do something with $_
}
Cheers!
__________________________________________________ ______________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
[url]http://www.messagelabs.com[/url]
__________________________________________________ ______________________
Stephen Hardisty Guest
-
John W. Krahn #4
Re: Updating an array within a hash
Jan Eden wrote:
The second argument of push() is a list so you don't need a for loop>
> Soumyadeep nandi wrote:
>>> >I am stuck in a problem for which I need your help. My
> >problem spins around adding an element in an array
> >within a hash.
> >I have a hash declared as $hash{"1"}=@array; now I
> >want to add an element to it within a loop. How would
> >I do this? I would be highly grateful to you if you
> >could help me.
> Your syntax puts a number (the number of elements in @array into $hash{"1"}.
>
> I guess you want to set an array reference as the hash key's value:
>
> $hash{"1"} = \@array;
>
> In that case, you can just
>
> push @{$hash{"1"}}, $_ for (...list goes here...);
>
> to push into the array referenced by $hash{"1"}.
there.
push @{$hash{"1"}}, (...list goes here...);
:-)
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Soumyadeep Nandi #5
Re: Updating an array within a hash
Hi Jan and Stephen,
I could solve my problem. The way I could do is as
follows:
push (@{$C{$range}}, $c);
where C is the hash having keys some integers and
values as an array. So I could add more elements($c)
to the array inside that hash(%C) is as above.
Thank you so much Stephen and Jan for help and
suggestions.
__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
[url]http://taxes.yahoo.com/filing.html[/url]
Soumyadeep Nandi Guest



Reply With Quote

