Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Possible unpack modification (or module)

by samurai (Monk)
on Sep 30, 2002 at 15:31 UTC ( #201744=perlmeditation: 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

Replies are listed 'Best First'.
Re: Possible unpack modification (or module)
by Juerd (Abbot) on Sep 30, 2002 at 16:14 UTC

    sub funpack { my ($template, $source) = @_; my @fields; push @fields, $2 while $template =~ s/(\D\d+)(\S+)/$1/; my $hash = {}; @$hash{@fields} = unpack $template, $source; return $hash } my $data = funpack 'A20name A1sex A3age A8salary A12sign', $foo;
    Completely untested, but I just HAD to mention "fun"pack.

    By the way, if you want a module that does this, just create it yourself and put it on CPAN. Make your world a better place, and possibly the worlds of others too.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

      Or, assuming a less ambiguous template format:
      sub funpack { my ($template, $source) = @_; my %hash = map { reverse split /:/, $_, 2 } split " ", $template; local $" = ' '; @hash{keys %hash} = unpack "@{[ values %hash ]}", $source; return \%hash; } my $data = funpack 'A20:name A1:sex A3:age A8:salary A12:sign', $foo;

      (Field names may start with a digit with this data format. I also find it easier on the eye.)

      Update: D'oh. Always test before posting.. Of course the above code doesn't preserve the order of fields, which is crucial.

      sub funpack { my ($template, $source) = @_; my ($i, @field, @format) = 0; push @{ (\@format, \@field)[$i++ & 1] }, $_ for map /^([^:]+):(.*)/, split " ", $template; local $" = " "; return { map +(shift(@format) => $_), unpack "@format", $source }; } my $data = funpack 'A20:name A1:sex A3:age A8:salary A12:sign', $foo;

      Makeshifts last the longest.

Re: Possible unpack modification (or module)
by premchai21 (Curate) on Sep 30, 2002 at 18:06 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://201744]
Approved by fireartist
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2023-03-21 02:32 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.