Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

add element to hash

by Anonymous Monk
on Sep 01, 2014 at 07:12 UTC ( [id://1099146]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, thank you so far for helping me with some beginner-errors. Now I have another question. I have an Hash, which Looks like this:
%hash{@key1} => [@array1, @array2, etc]; %hash{@key2} => [@array1, @array2, etc.];
Now I want to add a string at the end of the elements which belongs to the key. So at the end of the elements of key1, there should be a specific string. like this:
%hash{@key1} => [@array1, @array2, "abc"]; %hash{@key2} => [@array1, @array2, "abc"];
I tried it so far with this, but it did not work as expected.
push (@{$hash{$key}}, (@array1, "abc"));
I hope someone could help me! Regards sandorado

Replies are listed 'Best First'.
Re: add element to hash
by Athanasius (Archbishop) on Sep 01, 2014 at 07:43 UTC

    Hello sandorado,

    ...it did not work as expected isn’t a very helpful error description. The following is a guess at what you are trying to do:

    #! perl use strict; use warnings; use Data::Dump; my @array1 = qw( a e i o u ); my @array2 = qw( x y z ); my %hash = ( key1 => [ \@array1, \@array2, ], ); dd \%hash; push @{ $hash{key1} }, 'abc'; dd \%hash;

    Output:

    17:39 >perl 994_SoPW.pl { key1 => [["a", "e", "i", "o", "u"], ["x", "y", "z"]] } { key1 => [["a", "e", "i", "o", "u"], ["x", "y", "z"], "abc"] } 17:39 >

    If I’ve guessed wrong, you will need to clarify your question.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hey, thanks for your quick Respons. You understand what I mean, but it did not work properly. I will specify my question. I'm working with the GDS2Stream module of perl. So I try to create from an inputfile a gds-file recursivly. There is the beginning of one structure and the end of a structure. It Looks like this:
      Begin of structure-> #something to draw #anything else to draw #something to draw End of Structure Begin of structure-> #something to draw end of structure
      The begin of structure is a specific Name. And means the key of my hash. Every key contains rrays which are a list of every element I Need to draw within in this structure. The elements of the Array differs from key to key. So this means the first key is able to have about 10 elements and the second key is able to have about 50 elements;
      %hash{@structure_name1} => {[@draw1, @draw2,]}; %hash{@structure_name2} => {[@draw1, @draw2, @draw3]};
      And my idea was to find the last element of this Array in the structure and add the end of structure (equal with command printEndstr of the modul)like this:
      %hash{@structure_name1} => {[@draw1, @draw2,"$gds2File->printEndstr]}; %hash{@structure_name2} => {[@draw1, @draw2, @draw3, $gds2File->printE +ndstr]};
      The Problem is, that in gdsFile there are more Ends of structure and beginnings of structure. I Thing the Problem is the recursion or something like that

        You're incorrectly inserting braces, parenthesis and brackets at several points in your two posts. This really isn't helping you out. You don't seem to have a good handle on them, and they're very important to be able to understand how to access and use data structures in perl. I suggest you read perldsc, perlreftut, perllol and company.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Please take a close look at the code Athanasius (++) posted - I believe that is very close to what you are trying to do - except his code has the correct syntax.

        The only thing I can guess differently about how your structure might look is to replace the initial hash assignment with:

        my %hash = ( key1 => [ @array1, @array2, ], # Assign array elements, not refere +nces. );

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: add element to hash
by LanX (Saint) on Sep 01, 2014 at 11:59 UTC
    > %hash{@key1} => [@array1, @array2, "abc"];

    left side hash-slice syntax error => right side array-ref ...

    ...what???

    like roboticus already tried to explain this doesn't make much sense, and we can't guess what the real question is.

    Please show us real working code and use Data::Dumper to visualize the data ... that is input and desired output.

    And please have a look at How (Not) To Ask A Question

    • Only Post Relevant Code
    • Include Sample Data (I/O)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    striked syntax error

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1099146]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found