in reply to How to get binary value based on field value entered
I have some 6 field say field 1,field 2,field 3,field 4,field 5,field 6.
You'll have to clarify what you mean by "field"?
Are these substrings within a single string? Or entryfields within an HTML form? Columns in a CSV file?
And also what you mean by "blank"?
Let's say that you have captured the fields into an array, then you might build you binary value this way:
my @fields = ... ; my $binary = chr(0); defined( $fields[ $_ ] ) && length( $fields[ $_ ] ) and vec( $binary, +$_, 1 ) = 1 for 0 .. $#fields;
For example:
@fields = ( 'fred', undef, '', 0, 123, ' ' );; $binary = '';; vec( $binary, $_, 1 ) = ($fields[ $_ ] // '' ) =~ /\S/ ? 1 : 0 for 0 . +. $#fields;; print unpack 'b*', $binary;; 10011000
|
|---|