Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Yes it's weird, I just downloaded the code again, and it worked fine, but I still have the original file which gave me errors. I include it here, just because I can't see where the difference is? Maybe a } got clipped somewhere? It has to be Monday. :-)
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @AoH = ( { OutFile => 'albu.htm', PageId => '3.4.1.5.1', Menu => 'albu_menu.inc', Content => 'albu.inc' }, { OutFile => 'armi.php', PageId => '3.4.1.5.2', Menu => 'armi_menu.inc', Content => 'armi.inc' }, { OutFile => 'bank.htm', PageId => '3.4.1.5.3', Menu => 'bank_menu.inc', Content => 'bank.inc' }, { OutFile => 'bate.htm', PageId => '3.4.1.5.4', Menu => 'bate_menu.inc', Content => 'bate.inc' } ); my $benc = bEncode(\@AoH); print $benc,"\n"; my $bdec = bDecode(\$benc); print Dumper([\$bdec]),"\n"; # ##### # Provides fuctions for bencoding/decoding ### # Decodes a bencoded string to a hash/array reference sub bDecode { my $dictref = $_[0]; my $retref = undef; if (substr(${$dictref},0,1) eq "d") { $retref = {}; ${$dictref} = substr(${$dictref},1); PROCDICTHASH: while (substr(${$dictref},0,1) ne 'e') { my ($key, $value); ${$dictref} =~ /^(-?[\d]+)/; $key = substr(${$dictref}, length($1)+1, $1); ${$dictref} = substr(${$dictref}, length($1)+$ +1+1); if (${$dictref} =~ /^[ld]/) { $value = bDecode(\${$dictref}); if (!defined($value)) { return undef; } } elsif (${$dictref} =~ /^(-?[\d]+):/) { $value = substr(${$dictref}, length($1 +)+1, $1); ${$dictref} = substr(${$dictref}, leng +th($1)+$1+1); } elsif (${$dictref} =~ /^i(-?[\d]+)e/) { $value = $1; ${$dictref} = substr(${$dictref}, leng +th($1)+2); } else { return undef; } $retref->{$key} = $value; } ${$dictref} = substr(${$dictref},1); } elsif (substr(${$dictref},0,1) eq "l") { $retref = []; ${$dictref} = substr(${$dictref},1); PROCDICTARR: while (substr(${$dictref},0,1) ne 'e') { if (${$dictref} =~ /^[ld]/) { my $value = bDecode(\${$dictref}); if (!defined($value)) { return undef; } push (@$retref, $value); } elsif (${$dictref} =~ /^(-?[\d]+):/) { push (@$retref, substr(${$dictref}, le +ngth($1)+1, $1)); ${$dictref} = substr(${$dictref}, leng +th($1)+$1+1); } elsif (${$dictref} =~ /^i(-?[\d]+)e/) { push (@$retref, $1); ${$dictref} = substr(${$dictref}, leng +th($1)+2); } else { return undef; } } ${$dictref} = substr(${$dictref},1); } else { return undef; } return $retref; } #Encodes a hash/array ref to a bencoded string sub bEncode { my $dictref = $_[0]; my $retval = ''; # my ($key, $value); if (ref($dictref) eq 'HASH') { $retval = 'd'; for my $key (sort keys %$dictref) { $retval .= length($key).':'.$key; if (ref($dictref->{$key}) eq 'ARRAY' || ref($d +ictref->{$key}) eq 'HASH') { $retval .= bEncode($dictref->{$key}); } elsif ($dictref->{$key} =~ /^[\d]+$/) { $retval .= 'i'.$dictref->{$key}.'e'; } else { $retval .= length($dictref->{$key}).': +'.$dictref->{$key}; } } $retval .= 'e'; } elsif (ref($dictref) eq 'ARRAY') { $retval = 'l'; for (my $i = 0; $i <= $#{$dictref}; $i++) { if (ref($dictref->[$i]) eq 'ARRAY' || ref($dic +tref->[$i]) eq 'HASH') { $retval .= bEncode($dictref->[$i]); } elsif ($dictref->{$key} =~ /^[\d]+$/) { $retval .= 'i'.$dictref->[$i].'e'; } else { $retval .= length($dictref->[$i]).':'. +$dictref->[$i]; } } $retval .= 'e'; } return $retval; };

I'm not really a human, but I play one on earth. flash japh

In reply to Re^3: serialise/unserialising data structures by zentara
in thread serialise/unserialising data structures by monoxide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found