in reply to Re: How can I store two values with the same key in a hash?
in thread How can I store two values with the same key in a hash?

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^3: How can I store two values with the same key in a hash?
by davorg (Chancellor) on Oct 12, 2006 at 13:27 UTC
    It doesn't work.

    You know, that's really not a very useful bug report. In what way did it fail to work? What unexpected behaviour did you see?

    Using this code, the values in your hash are no longer scalars, they have become array references. Did you make the changes required to deal with that?

    You need to understand the changes that you are making to your code. Don't just copy code from the web without knowing what it does.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re^3: How can I store two values with the same key in a hash?
by jdporter (Paladin) on Oct 12, 2006 at 13:58 UTC
    When i change the code from $cnf{$c}=$_; to push @{$cnf{$c} }, $_; It doesn't work.

    MJD had some things to say about that:

    • You cannot just paste code with no understanding of what is going on and expect it to work.
    • Of course it doesn't work! That's because you don't know what you are doing!
    And also:
    • In my experience that is a bad strategy, because the people who ask such questions are the ones who paste the answer into their program without understanding it and then complain that it 'does not work'.
    • You said 'It doesn't work'. The next violation will be punished by death.

    We're building the house of the future together.
Re^3: How can I store two values with the same key in a hash?
by blazar (Canon) on Oct 12, 2006 at 13:32 UTC
    When i change the code from $cnf{$c}=$_; to push @{$cnf{$c} }, $_; It doesn't work.
    1. Please put your code in code tags, and normal text outside of them;
    2. What doesn't work?
    Suggest me some suitable code.

    What I suggested you seems suitable enough, as the following minimal adaptation of your code shows:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @fields=split /\n/, <<'.END'; "INVITE sip:paka@36.212.176.92 SIP/2.0 To:samir <sip:paka@36.212.176.92> Via: SIP/2.0/udp 36.212.176.90:5060;branch=z9hG4bk_Vn Via: SIP/2.0/udp 36.212.176.66:5060;branch=z9hG4bk_oo From: sanjay<sip:sanjay@36.212.176.90> Call-ID: _Vn8TZTk2H@36.212.176.90 CSeq: 215 INVITE Max-Forwards: 70 Contact: sanjay<sip:sanjay@36.212.176.90:5060>"; .END my %config = ( URI => { regex => qr/^INVITE\ssip:/}, To => { regex => '^To:'}, From => { regex => '^From:'}, Via => { regex => '^Via:'}, Call_ID => { regex => '^Call-ID:'}, CSeq => { regex => '^CSeq:'}, MaxForwards => { regex => '^Max-Forwards:'}, Contact => { regex => '^Contact:'}, ); my %cnf; foreach (@fields) { foreach my $c (keys %config) { if (/$config{$c}{regex}/) { push @{ $cnf{$c} }, $_; } } } print Dumper $cnf{Via}; __END__

    Please note that I've never suggested you to blindily make that simple substitution and expect your code to work, but to change its logic in agreement with that change instead, nor am I claiming now that this is the best way to parse those headers or that I would do it like that. I hope to have given you a clue instead. Of course it won't be of much use unless you make the minimum effort to get acquainted with some basic perl first.