Need help/advice to improve script

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::Dumper;
print Data::Dumper->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:eek:[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::Dumper;

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:eek:[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,
 
P

Peter J. Holzer

$line =~ /^
([^:]*)
: (?<pass> [^:]*)
: (?<emails> [^:]*)
:? (?<flag> [^:]*)
:? (?<pass_change> [^:]*)
:? (?<flag_change> .*)
$/x
and $data->{$1} = {%+};

That's nice. I guess one of these days I should try to get rid of 5.8.x
so that I can finally start to use 5.10 features ...

hp
 
S

sln

$line =~ /^
([^:]*)
: (?<pass> [^:]*)
: (?<emails> [^:]*)
:? (?<flag> [^:]*)
:? (?<pass_change> [^:]*)
:? (?<flag_change> .*)
$/x
and $data->{$1} = {%+};

That's nice. I guess one of these days I should try to get rid of 5.8.x
so that I can finally start to use 5.10 features ...

hp

Named capture buffers are a nice feature, made for this kind of thing.

-sln
 
S

sopan.shewale

    $line =~ /^
        ([^:]*)
      :  (?<pass>   [^:]*)
      :  (?<emails> [^:]*)
      :? (?<flag>   [^:]*)
      :? (?<pass_change> [^:]*)
      :? (?<flag_change> .*)
    $/x
    and $data->{$1} = {%+};

That's nice. I guess one of these days I should try to get rid of 5.8.x
so that I can finally start to use 5.10 features ...

        hp

[sopan] - this is interesting feature and not sure if it will work on
old versions of Perl.
 
S

sln

    $line =~ /^
        ([^:]*)
      :  (?<pass>   [^:]*)
      :  (?<emails> [^:]*)
      :? (?<flag>   [^:]*)
      :? (?<pass_change> [^:]*)
      :? (?<flag_change> .*)
    $/x
    and $data->{$1} = {%+};

That's nice. I guess one of these days I should try to get rid of 5.8.x
so that I can finally start to use 5.10 features ...

        hp

[sopan] - this is interesting feature and not sure if it will work on
old versions of Perl.

The original point was on fixing the regular expression to provide default ''
instead of undef(ined). Split is nice, its simple and mostly dumb. It uses
regular expressions as well, but with the focus on a partition, not the data.
Ok, here you go ... compatible with v5.8. It doesen't look too different from
the named capture buffers does it? It keeps a clear, readable, and modifyable
form.

The REAL advantage of named capture buffers is that there is no need to keep
track of sequentially numbered capture buffers. You can modify the regular
expression, insert/extract sub-expressions without the need to worry about
modifying the code body that uses the result.

-sln

-------------------
use strict;
use warnings;
use Data::Dumper;

my $data = {};

while (defined (my $line = <DATA>)) {
chomp $line;
$line =~ /^([^:]*):([^:]*):([^:]*):?([^:]*):?([^:]*):?(.*)$/
and $data->{$1} = {
pass =>$2,
emails =>$3,
flag =>$4,
pass_change =>$5,
flag_change =>$6
};
}

print Dumper($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:eek:[email protected]:0
RestMissing:ZuqpLZ7AxHBvw:[email protected]
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top