insert anonymous hash into anonymous hash for counting in a loop










0















I'm trying to count starts and stops of some services i keep track of in logs.
I'm not going to past here entire code, but my way of doing hash is this:
I'm passing those starts and stops into anonymous hash .
First I'm creating anonymous hash filled with keys and values (in my case $knot is a key an zeros are values). Next im replaqcing values with another hash.
My code looks like this:



foreach $knot (@knots) 
chomp $knot;
$variable = $variable."$knot;0;";

$Services = split(/;/,$variable);


my $data =

Starts=>'0',
Stops=>'0',
;

foreach my $key (keys %$Services)
$Services->$key = $data;

print Dumper $Services;


Printing shows:



$VAR1 = {
' knot1' =>
'Stops' => '0',
'Starts' => '0'
,
' knot2' => $VAR1->' knot1',
' knot3' => $VAR1->' knot1',
' knot4' => $VAR1->' knot1',
' knot5' => $VAR1->' knot1',


and so on. Is there a better way of doing this? My way if i'm correct is badly written because changing knot1 starts/stops changes every other knot values.










share|improve this question



















  • 3





    You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

    – Corion
    Nov 14 '18 at 13:01















0















I'm trying to count starts and stops of some services i keep track of in logs.
I'm not going to past here entire code, but my way of doing hash is this:
I'm passing those starts and stops into anonymous hash .
First I'm creating anonymous hash filled with keys and values (in my case $knot is a key an zeros are values). Next im replaqcing values with another hash.
My code looks like this:



foreach $knot (@knots) 
chomp $knot;
$variable = $variable."$knot;0;";

$Services = split(/;/,$variable);


my $data =

Starts=>'0',
Stops=>'0',
;

foreach my $key (keys %$Services)
$Services->$key = $data;

print Dumper $Services;


Printing shows:



$VAR1 = {
' knot1' =>
'Stops' => '0',
'Starts' => '0'
,
' knot2' => $VAR1->' knot1',
' knot3' => $VAR1->' knot1',
' knot4' => $VAR1->' knot1',
' knot5' => $VAR1->' knot1',


and so on. Is there a better way of doing this? My way if i'm correct is badly written because changing knot1 starts/stops changes every other knot values.










share|improve this question



















  • 3





    You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

    – Corion
    Nov 14 '18 at 13:01













0












0








0








I'm trying to count starts and stops of some services i keep track of in logs.
I'm not going to past here entire code, but my way of doing hash is this:
I'm passing those starts and stops into anonymous hash .
First I'm creating anonymous hash filled with keys and values (in my case $knot is a key an zeros are values). Next im replaqcing values with another hash.
My code looks like this:



foreach $knot (@knots) 
chomp $knot;
$variable = $variable."$knot;0;";

$Services = split(/;/,$variable);


my $data =

Starts=>'0',
Stops=>'0',
;

foreach my $key (keys %$Services)
$Services->$key = $data;

print Dumper $Services;


Printing shows:



$VAR1 = {
' knot1' =>
'Stops' => '0',
'Starts' => '0'
,
' knot2' => $VAR1->' knot1',
' knot3' => $VAR1->' knot1',
' knot4' => $VAR1->' knot1',
' knot5' => $VAR1->' knot1',


and so on. Is there a better way of doing this? My way if i'm correct is badly written because changing knot1 starts/stops changes every other knot values.










share|improve this question
















I'm trying to count starts and stops of some services i keep track of in logs.
I'm not going to past here entire code, but my way of doing hash is this:
I'm passing those starts and stops into anonymous hash .
First I'm creating anonymous hash filled with keys and values (in my case $knot is a key an zeros are values). Next im replaqcing values with another hash.
My code looks like this:



foreach $knot (@knots) 
chomp $knot;
$variable = $variable."$knot;0;";

$Services = split(/;/,$variable);


my $data =

Starts=>'0',
Stops=>'0',
;

foreach my $key (keys %$Services)
$Services->$key = $data;

print Dumper $Services;


Printing shows:



$VAR1 = {
' knot1' =>
'Stops' => '0',
'Starts' => '0'
,
' knot2' => $VAR1->' knot1',
' knot3' => $VAR1->' knot1',
' knot4' => $VAR1->' knot1',
' knot5' => $VAR1->' knot1',


and so on. Is there a better way of doing this? My way if i'm correct is badly written because changing knot1 starts/stops changes every other knot values.







perl hash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 16:28









Boris Däppen

658517




658517










asked Nov 14 '18 at 12:28









Marcin KMarcin K

31




31







  • 3





    You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

    – Corion
    Nov 14 '18 at 13:01












  • 3





    You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

    – Corion
    Nov 14 '18 at 13:01







3




3





You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

– Corion
Nov 14 '18 at 13:01





You are only ever assigning the same reference $data. The easiest approach to make them different between each other is to move my $data = ...; into the loop.

– Corion
Nov 14 '18 at 13:01












1 Answer
1






active

oldest

votes


















0














Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:



use Data::Dumper;

my %hash = ();

$hashapplegreen++;
$hashapplered ++;
$hashpearyellow++;
$hashapplegreen++;
$hashapplered ++;
$hashapplegreen++;

print Dumper(%hash);


This will produce the desired structure for counting:



$VAR1 = 
'apple' =>
'green' => 3,
'red' => 2
,
'pear' =>
'yellow' => 1

;


This also works in loops using variables (here using a reference to a hash):



my $hash_ref = ;
for my $fruit (qw( apple pear apple peach apple pear ))
$hash_ref->$fruit++;

print Dumper($hash_ref);


resulting in:



$VAR1 = 
'peach' => 1,
'pear' => 2,
'apple' => 3
;





share|improve this answer




















  • 1





    my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

    – melpomene
    Nov 16 '18 at 14:06











  • Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

    – Marcin K
    Nov 22 '18 at 11:06











  • @MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

    – Boris Däppen
    Nov 26 '18 at 12:17











  • @Boris Däppen Done :)

    – Marcin K
    Nov 27 '18 at 14:28










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300266%2finsert-anonymous-hash-into-anonymous-hash-for-counting-in-a-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:



use Data::Dumper;

my %hash = ();

$hashapplegreen++;
$hashapplered ++;
$hashpearyellow++;
$hashapplegreen++;
$hashapplered ++;
$hashapplegreen++;

print Dumper(%hash);


This will produce the desired structure for counting:



$VAR1 = 
'apple' =>
'green' => 3,
'red' => 2
,
'pear' =>
'yellow' => 1

;


This also works in loops using variables (here using a reference to a hash):



my $hash_ref = ;
for my $fruit (qw( apple pear apple peach apple pear ))
$hash_ref->$fruit++;

print Dumper($hash_ref);


resulting in:



$VAR1 = 
'peach' => 1,
'pear' => 2,
'apple' => 3
;





share|improve this answer




















  • 1





    my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

    – melpomene
    Nov 16 '18 at 14:06











  • Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

    – Marcin K
    Nov 22 '18 at 11:06











  • @MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

    – Boris Däppen
    Nov 26 '18 at 12:17











  • @Boris Däppen Done :)

    – Marcin K
    Nov 27 '18 at 14:28















0














Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:



use Data::Dumper;

my %hash = ();

$hashapplegreen++;
$hashapplered ++;
$hashpearyellow++;
$hashapplegreen++;
$hashapplered ++;
$hashapplegreen++;

print Dumper(%hash);


This will produce the desired structure for counting:



$VAR1 = 
'apple' =>
'green' => 3,
'red' => 2
,
'pear' =>
'yellow' => 1

;


This also works in loops using variables (here using a reference to a hash):



my $hash_ref = ;
for my $fruit (qw( apple pear apple peach apple pear ))
$hash_ref->$fruit++;

print Dumper($hash_ref);


resulting in:



$VAR1 = 
'peach' => 1,
'pear' => 2,
'apple' => 3
;





share|improve this answer




















  • 1





    my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

    – melpomene
    Nov 16 '18 at 14:06











  • Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

    – Marcin K
    Nov 22 '18 at 11:06











  • @MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

    – Boris Däppen
    Nov 26 '18 at 12:17











  • @Boris Däppen Done :)

    – Marcin K
    Nov 27 '18 at 14:28













0












0








0







Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:



use Data::Dumper;

my %hash = ();

$hashapplegreen++;
$hashapplered ++;
$hashpearyellow++;
$hashapplegreen++;
$hashapplered ++;
$hashapplegreen++;

print Dumper(%hash);


This will produce the desired structure for counting:



$VAR1 = 
'apple' =>
'green' => 3,
'red' => 2
,
'pear' =>
'yellow' => 1

;


This also works in loops using variables (here using a reference to a hash):



my $hash_ref = ;
for my $fruit (qw( apple pear apple peach apple pear ))
$hash_ref->$fruit++;

print Dumper($hash_ref);


resulting in:



$VAR1 = 
'peach' => 1,
'pear' => 2,
'apple' => 3
;





share|improve this answer















Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:



use Data::Dumper;

my %hash = ();

$hashapplegreen++;
$hashapplered ++;
$hashpearyellow++;
$hashapplegreen++;
$hashapplered ++;
$hashapplegreen++;

print Dumper(%hash);


This will produce the desired structure for counting:



$VAR1 = 
'apple' =>
'green' => 3,
'red' => 2
,
'pear' =>
'yellow' => 1

;


This also works in loops using variables (here using a reference to a hash):



my $hash_ref = ;
for my $fruit (qw( apple pear apple peach apple pear ))
$hash_ref->$fruit++;

print Dumper($hash_ref);


resulting in:



$VAR1 = 
'peach' => 1,
'pear' => 2,
'apple' => 3
;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 13:07

























answered Nov 16 '18 at 7:52









Boris DäppenBoris Däppen

658517




658517







  • 1





    my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

    – melpomene
    Nov 16 '18 at 14:06











  • Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

    – Marcin K
    Nov 22 '18 at 11:06











  • @MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

    – Boris Däppen
    Nov 26 '18 at 12:17











  • @Boris Däppen Done :)

    – Marcin K
    Nov 27 '18 at 14:28












  • 1





    my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

    – melpomene
    Nov 16 '18 at 14:06











  • Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

    – Marcin K
    Nov 22 '18 at 11:06











  • @MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

    – Boris Däppen
    Nov 26 '18 at 12:17











  • @Boris Däppen Done :)

    – Marcin K
    Nov 27 '18 at 14:28







1




1





my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

– melpomene
Nov 16 '18 at 14:06





my %hash = (); is equivalent to my %hash; %hash = ();, which is redundant because my already creates the hash empty.

– melpomene
Nov 16 '18 at 14:06













Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

– Marcin K
Nov 22 '18 at 11:06





Thank you, this was helpful , i have made loop with added your code and it works like a charm :)

– Marcin K
Nov 22 '18 at 11:06













@MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

– Boris Däppen
Nov 26 '18 at 12:17





@MarcinK nice! Glad I could be of help. Could you please accept the answer as your valid answer by clicking the checkmark at the left side of this answer. This will help you, me and all the other stack users. thx

– Boris Däppen
Nov 26 '18 at 12:17













@Boris Däppen Done :)

– Marcin K
Nov 27 '18 at 14:28





@Boris Däppen Done :)

– Marcin K
Nov 27 '18 at 14:28

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300266%2finsert-anonymous-hash-into-anonymous-hash-for-counting-in-a-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric