Hi Anirban

'unless ($dev)' is just the same as 'if(not ($dev))' and it means 'if this
string (from which I've removed all the spaces) is empty do ... '

Does that answer your question?

Andrew



On Thu, Apr 2, 2015 at 5:05 PM, Anirban Adhikary <anirban.adhik...@gmail.com
> wrote:

> Hi Andrew
>
> From your code I have completed the code and it is working fine. I am
> pasting here the final code.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> 1 #!/usr/bin/perl
>   2
>   3 use strict;
>   4 use warnings;
>   5
>   6 my $Input_File_Name = "inputdir/MAT4BE_SC_STD_INFO";
>   7 open my $RFH,'<',$Input_File_Name;
>   8 while(<$RFH>) {
>   9         next if $_ =~ /^\s*$/;
>  10         my $line = $_;
>  11         if ( $line =~ m/^\s+\d+/ ) {
>  12                 $line =~ s/^\s+//;
>  13                 $line =~ s/\s+$//;
>  14                 #next unless $line;
>  15                 my $dev = substr($line, 10, 15);
>  16                 my $dev1 = substr($line, 15, 20);
>  17                 $dev =~ s/\s*//g;
>  18                 $dev1 =~ s/\s*//g;
>  19                         unless ($dev) {
>  20                                 $line =~ s/(..........)(.....)/$1NULL/;
>  21                                 }
>  22
>  23                         unless ($dev1) {
>  24                                 $line =~
> s/(...............)(.....)/$1NULL/;
>  25                                 }
>  26                         print $line."\n";
>  27
>  28                 #}
>  29         }
>  30 }
>  31 close($RFH);
>
> My Request to you can you please explain from line number 19-25. Specially
> the pattern matching sections and also the significance of unless and why u
> have used unless not any other.
>
> Best Regards
> Anirban.
>
>
> On Thu, Apr 2, 2015 at 9:15 PM, Andrew Solomon <and...@geekuni.com> wrote:
>
>> Or more politely... :)
>>
>> https://gist.github.com/anonymous/2ebb0441bcdec4a94e48
>>
>>
>>
>> On Thu, Apr 2, 2015 at 4:43 PM, Andrew Solomon <and...@geekuni.com>
>> wrote:
>>
>>> Hi Anirban
>>>
>>> I haven't completely solved the problem - only addressing the DEV field,
>>> but I hope this points you in the right direction!
>>>
>>> cheers
>>>
>>> Andrew
>>>
>>> #!/usr/bin/env perl
>>>
>>> use strict;
>>>
>>> use warnings;
>>>
>>>
>>>
>>> # NOTE This assumes the space aren't tabs and the columns are aligned
>>>
>>>
>>> my $input = '
>>>
>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>
>>> 1      0                                 31      1
>>>
>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>
>>> 2      0                                 31      1
>>>
>>> ';
>>>
>>>
>>> my @input_lines = split /\n/, $input;
>>>
>>>
>>>
>>> foreach my $line (@input_lines) {
>>>
>>>     next unless $line;
>>>
>>>     my $dev = substr($line, 10, 15);
>>>
>>>     $dev =~ s/\s*//g;
>>>
>>>     unless ($dev) {
>>>
>>>         $line =~ s/(..........)(.....)/$1UNDEF/;
>>>
>>>     }
>>>
>>>     print $line."\n";
>>>
>>>
>>> }
>>>
>>> On Thu, Apr 2, 2015 at 3:43 PM, Anirban Adhikary <
>>> anirban.adhik...@gmail.com> wrote:
>>>
>>>> Hi List
>>>>
>>>> I would like to process a file which has the following structure.
>>>>
>>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>>    1        0                                             31
>>>> 1
>>>>
>>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>>    2        0                                             31
>>>> 1
>>>>
>>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>>    3        0                                              31
>>>> 1
>>>>
>>>> SCGR  SC  DEV            DEV1           NUMDEV  DCP  STATE  REASON
>>>>    4        0                                              31
>>>> 1
>>>>
>>>> Condition is if any field is empty (In this case DEV and DEV1)  those
>>>> has to be replaced by the keyword "NULL". When I am trying to split the
>>>> line based on whitespace
>>>> @elements = split(/\s+/,$line);
>>>> in $element[2] entire blank space is coming upto NUMDEV1. so while
>>>> printing I am not getting the proper output.
>>>>
>>>> How to resolve this problem?
>>>>
>>>> Best Regards
>>>> Anirban.
>>>>
>>>
>>>
>>>
>>> --
>>> Andrew Solomon
>>>
>>> Mentor@Geekuni http://geekuni.com/
>>> http://www.linkedin.com/in/asolomon
>>>
>>
>>
>>
>> --
>> Andrew Solomon
>>
>> Mentor@Geekuni http://geekuni.com/
>> http://www.linkedin.com/in/asolomon
>>
>
>


-- 
Andrew Solomon

Mentor@Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon

Reply via email to