sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: How can I store two values with the same key in a hash?
by blazar (Canon) on Oct 12, 2006 at 10:40 UTC
    How can i store two Via in the two keys(Via and Via1) of hash. So that when i will give print $cnf{Via};
    1. PLEASE put the subject of your posts in their Subject;
    2. The two "Via" lines match the same regex, thus I would change the logic of the program to record all possible matches instead of storing the last one, i.e. possibly change
      $cnf{$c}=$_;
      to
      push @{ $cnf{$c} }, $_;
      of course you may need a more complex setup such that entries that can only occur once are stored as plain scalars and entries that can occur multiple times are stored as arrayrefs.
    3. the code you posted seems to suggests the reuse of someone else's code with very few clues about what it really does, thus I recommend getting acquainted with at least the very basics of Perl as you may find in any introductory book or tutorial.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can I store two values with the same key in a hash?
by cephas (Pilgrim) on Oct 12, 2006 at 15:58 UTC
    First, I'll answer the question directly:
    #!/usr/bin/perl use strict; use Data::Dumper; my $msg= "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>"; my @fields = split /\n/, $msg; my %config = ( URI => { regex => qr/^INVITE\ssip:/}, To => { regex => '^To:'}, From => { regex => '^From:'}, Via => { regex => '^Via:'}, Via1 => { regex => '^Via:'}, Call_ID => { regex => '^Call-ID:'}, CSeq => { regex => '^CSeq:'}, MaxForwards => { regex => '^Max-Forwards:'}, Contact => { regex => '^Contact:'}, ); my %cnf; foreach (@fields) { chomp; foreach my $c (sort keys %config) { if (/$config{$c}{regex}/) { next if exists $cnf{$c}; $cnf{$c} = $_; last; } } } print Dumper \%cnf;

    That will give you what you asked for. The only change was in the for loop.

    Now, there are a lot of very helpful people here. They tried to give you what is a more appropriate solution. This is not a place to come if you want people to solve all of your problems for you. If you would like someone to write all of your code for you, I recommend you hire a competent contractor. Demanding working code from people is not likely to go over well. I'm upvoting the people that tried to steer you in the right direction, and giving my rare downvote to the node where you demanded a different solution.