Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
When playing around with PHP for web development, there was an interesting feature I thought might be neat for perl to look at possibly including that was in PHP. It involves unpacking data.

For example, right now I do a lot of this:

my @fields = qw/ name sex age salary sign /; my $unpack_tpl = "A20 A1 A3 A8 A12"; my %data = (); @data{ @fields } = unpack $unpack_tpl, $string; # { name => "Ted", sex => "M" ... }

to get hashed access to fixed-length data I'm parsing. But a neat feature of PHP is that you can supply the "field names" so to speak in the template itself. For example, perl pseudocode:

my $unpack_tpl = "A20name A1sex A3age, A8salary, A12sign"; my %data = unpack2 $unpack_tpl, $string;

I believe the second $unpack_tpl variable is more obvious as to what its doing than the original qw// list + unpack template version.

I think this "unpack2" function would easily be able to tell if we were trying to do the special hashed version (returning key=>val instead of just val) based on whether the field specification had trailing non-whitespace to be used as the keys.

Granted, I'm not talking about replacing unpack or making it part of the language spec (although the speed from it being in C would be nice), I'm thinking about toying with creating a module that might be able to do this kind of unpacking.

Before I get started, can anyone see any inherent flaws with this kind of data unpacking, or perhaps come up with a reason why this might be An Exremely Bad Idea®?

--
perl: code of the samurai


In reply to Possible unpack modification (or module) by samurai

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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found