<> wrote in comp.lang.perl.misc:
> Hello All!
> I have following problem:
> The file contain structured lines
> AAAAA BBBBB CCCCC DDDDD
> AAAAA BBBBB CCCCC DDDDD
> AAAAA CCCCC DDDDD
> AAAAA BBBBB CCCCC DDDDD
> AAAAA BBBBB DDDDD
> BBBBB CCCCC DDDDD
> AAAAA BBBBB CCCCC DDDDD
> I want save info into hash
> For example for third line
> $hash{one} = AAAAA
> $hash{two} = undef
> $hash{three} = CCCCC
> $hash{four} = DDDDD
I'd use an array for that, not a hash, but that's a minor detail.
> I know how do that using substr(), but this is not very elegantly!
If you find yourself calling substr() on the same string a lot, consider
using unpack(). Fixed-format data is likewise an invitation to use
unpack().
> Using regular expressions or split is inpossible, because lines contain
> empty elements!!!
You certainly *could* use a regex (and probably a tricky split too),
but unpack() is the tool for the job.
#!/usr/bin/perl
use strict; use warnings; $| = 1;
use Data:

umper;
while ( <DATA> ) {
my %h;
@h{ qw( one two three four)} = unpack 'a5xa5xa5xa5', $_;
$_ eq ' ' and $_ = undef for values %h; # blank fields
print Dumper \ %h;
}
__DATA__
AAAAA BBBBB CCCCC DDDDD
AAAAA BBBBB CCCCC DDDDD
AAAAA CCCCC DDDDD
AAAAA BBBBB CCCCC DDDDD
AAAAA BBBBB DDDDD
BBBBB CCCCC DDDDD
AAAAA BBBBB CCCCC DDDDD
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.