Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Troubles with Complex Data Structures

by madhatter (Sexton)
on Jan 23, 2001 at 02:54 UTC ( #53608=perlquestion: print w/replies, xml ) Need Help??
madhatter has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get information from port 43030 and forward its contents to other IP:Ports. My problem is that the loop runs twice, but uses the first "forward to" IP and port both times.

I can receive data fine, and it forwards information (be it twice to the same IP/port, but my socket code _does_ work)

$CFG{listen_port} = 43030; %data = ( '' => [ ["","43035"], ["","43040"], ], ); $socket = IO::Socket::INET->new(LocalPort => $CFG{listen_port}, Type = +> SOCK_DGRAM, Proto => 'udp') || die "ERROR: can't open socket\n"; while ($socket->recv($in, 1024) ) { $ip = inet_ntoa($socket->peeraddr); # print "Info from $ip"; foreach(@{$data{$ip}}){ my $tip = ${$_}[0]; $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => +$data{$ip}[$tip][1], PeerAddr => $data{$ip}[$tip][0]); $sock->send($in) or die "send: $!"; print "Sent to $data{$ip}[$tip][0]:$data{$ip}[$tip][1]\n"; } print $in; }

I don't usually mess with complex data structures because of problems like this. Once I get this worked out though, and I can loop through different layers, I think they'll come in handy.

Any help on how to get it to loop properly would be greatly appreciated. Note: I'm using extra variables to try to make things easier. ($tip = temporary IP) I realize the code sucks. I haven't cleaned it up yet.

Thanks for any help you could offer,

Replies are listed 'Best First'.
Re: Troubles with Complex Data Structures
by chipmunk (Parson) on Jan 23, 2001 at 03:10 UTC
    $tip will hold an IP address, but you're using it as an array index.

    Here's how I might write the foreach loop:

    foreach my $ip_port (@{$data{$ip}}) { # $ip_port = [""," +43035"]; $sock = IO::Socket::INET->new(Proto => 'udp', PeerAddr => $ip_port->[0], PeerPort => $ip_port->[1] ); $sock->send($in) or die "send: $!"; print "Sent to $ip_port->[0]:$ip_port->[1]\n"; }
    The trick here is that, once you have a reference to a sublevel of the data structure -- in this case $ip_port, which points to one of the inner arrays -- you can work directly from that reference.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://53608]
Approved by root
[shmem]: Lanx: that just enables the border. Looking up style attributes...
[LanX]: ?
[karlgoethebier]: code><table style="border: 1px solid black;}"></code> same for th and td ?
[shmem]: my scratchpad
[karlgoethebier]: oops!
[shmem]: karlgoethebier: np, got it. But doesn't either :-/
[shmem]: just leaving it as is. After all, enough effort put into that. hrmpf.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-07-24 16:44 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (356 votes). Check out past polls.