Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multiple substr() exchange

Reply
Thread Tools

Multiple substr() exchange

 
 
dima@inotech.ru
Guest
Posts: n/a
 
      09-02-2005
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???

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-02-2005
wrote:
> 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:umper;

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

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-02-2005
<> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MCSE 2003 w/Exchange or MCITP 2008 w/Exchange Juan MCITP 3 06-19-2009 07:29 PM
Cannot send mail from Exchange 2007 mailbox to Exchange 2003 mailb =?Utf-8?B?am9zdWU=?= MCSE 3 08-15-2007 12:02 PM
Exchange Links < Western Cartoon Cards > Exchange Links www.westerncartooncards.ca HTML 2 07-12-2004 07:59 PM
Exchange 5.5 with Exchange 2000 Cluster problem Jose Luis Microsoft Certification 0 02-13-2004 10:23 AM
help: My exchange lost M: and exchange can not start winman MCSE 9 07-30-2003 04:34 PM



Advertisments