Multiple substr() exchange

D

dima

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 know how do that using substr(), but this is not very elegantly!

Using regular expressions or split is inpossible, because lines contain
empty elements!!!

What solution is optimal for me???
Maybe any CPAN modules???
 
P

Paul Lalli

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 know how do that using substr(), but this is not very elegantly!

Using regular expressions or split is inpossible, because lines contain
empty elements!!!

You have an odd definition of "impossible".

Loop through the line, examine the each set of 5 characters. If any of
them are not a space character, put that string in the hash.
Otherwise, put undef in the hash.
What solution is optimal for me???

Here's one to get you started. Modify as needed:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @lines;
while (<DATA>){
my %line;
@line{qw/one two three four/} = map { /\S/ ? $_ : undef }
/(.{5}).?/g;
push @lines, \%line;
}

print Dumper(\@lines);


__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


Output:
$VAR1 = [
{
'one' => 'AAAAA',
'three' => 'CCCCC',
'two' => 'BBBBB',
'four' => 'DDDDD'
},
{
'one' => 'AAAAA',
'three' => 'CCCCC',
'two' => 'BBBBB',
'four' => 'DDDDD'
},
{
'one' => 'AAAAA',
'three' => 'CCCCC',
'two' => undef,
'four' => 'DDDDD'
},
{
'one' => 'AAAAA',
'three' => 'CCCCC',
'two' => 'BBBBB',
'four' => 'DDDDD'
},
{
'one' => 'AAAAA',
'three' => undef,
'two' => 'BBBBB',
'four' => 'DDDDD'
},
{
'one' => undef,
'three' => 'CCCCC',
'two' => 'BBBBB',
'four' => 'DDDDD'
},
{
'one' => 'AAAAA',
'three' => 'CCCCC',
'two' => 'BBBBB',
'four' => 'DDDDD'
}
];


Paul Lalli
 
A

Anno Siegel

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::Dumper;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top