in reply to How to get binary value based on field value entered

Hello rahularora, and welcome to the Monastery!

Here is one way:

#! perl use strict; use warnings; my @fields = (42, undef, 'abc', undef, 'foo', 'bar'); my $flags = 0; my $mask = 0b1000_0000; for my $field (@fields) { $flags |= $mask if defined $field; $mask >>= 1; } printf "%08b\n", $flags;

Output:

13:35 >perl 640_SoPW.pl 10101100 13:36 >

See Bitwise String Operators and Shift Operators.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to get binary value based on field value entered
by rahularora (Initiate) on Jun 06, 2013 at 05:19 UTC
    Thanks for quick reply. This forum is best forum for any perl related queries.and you peoples are very helpful.