S
sopan.shewale
Appreciate every one for responding to this question.The solution with split is also good idea. I am sticking to following
solution.
#!/usr/bin/perl
use strict;
use warnings;my $data = {};for (<DATA>) {
chomp;
my $line = $_;if ( defined $line ) {
if ( $line =~ /^([^:]*)[^:]*)[^:]*)(?:[^:]*)(?:[^:]*)
(?:[^:]*)(?:.*))?)?)?)?$/ )
^^^^^^^
The pattern doesn't change in form, therefore this is not necessary.
The switch from .*? to [^:]* was good, but why the final .*
$data->{$1}->{pass} = $2;
# ^^ $1 will never be '' or undef...
# in that case there is issue with password line
$data->{$1}->{emails} = $3 || '';
$data->{$1}->{flag} =
( ( defined $4 ) && ( $4 == 0 ) ) ? 0 : ( $4 || '' );
# we are assumming $4 as 0 or 1
$data->{$1}->{pass_change} = $5 || '';
$data->{$1}->{flag_change} = $6 || '';
}
}
use Data:umper;
print Data:umper->Dump( [$data] );__DATA__
AllPresent:hjliEO35kCgwI:[email protected]:1:23232:24324
LastMissing:CyL92g3OKi.jM:[email protected]:1:2323
FlagZero:CyL92g3OKi.jM:[email protected]:0:23232
OnlyFlag:ZuqpLZ7AxHBvw[email protected]:0
RestMissing:ZuqpLZ7AxHBvw:[email protected]
-----------------------------------------------------------
I got the help on regex from Peter Thoeny (http://twiki.org)
The part of this code is checked-in into TWiki repository
thanks every one for helping
I just got to scratch my head on this one.
If you can't use split, use a regex like below that will assign
'' to shorter lines. That way you don't have to do all that
define(d) checking.
-sln
------------------------
use strict;
use warnings;
use Data:umper;
my $data = {};
while (defined (my $line = <DATA>)) {
chomp $line;
$line =~ /^
([^:]*)
: (?<pass> [^:]*)
: (?<emails> [^:]*)
:? (?<flag> [^:]*)
:? (?<pass_change> [^:]*)
:? (?<flag_change> .*)
$/x
and $data->{$1} = {%+};
}
print Dumper($data);
__DATA__
AllPresent:hjliEO35kCgwI: (e-mail address removed):1:23232:24324
LastMissing:CyL92g3OKi.jM:[email protected]:1:2323
FlagZero:CyL92g3OKi.jM:[email protected]:0:23232
OnlyFlag:ZuqpLZ7AxHBvw[email protected]:0
RestMissing:ZuqpLZ7AxHBvw:[email protected]
This works too.. but with community, more people understand very
simple solution provided by split, so i am kind of inclined to use
split. We never know who wants to modify/own the code in future.
But i appreciate the solution provided by you. Thank you,
If you have bandwidth - you are most welcome to contribute in http://twiki.org
Cheers,