ankityad has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am solving a problem where i have dumped the data into a text file with comma separated registers(flip-flops) and their values. I want to check which registers can have multiple values and which are having constant values throughout. The sample values can be shown below: register1, 000111 register2, 010101 register1, 000111 register2, 011011 register4, 100000 register4, 100000 register5, 000000 register1, 000111 Now, I want to get the separate list of registers which are variable and there values and registers which have only constant values. Please help , how can I proceed with this problem?
  • Comment on finding the varaible quantity and static quantity from comma separated data and values

Replies are listed 'Best First'.
Re: finding the varaible quantity and static quantity from comma separated data and values
by choroba (Cardinal) on Jul 13, 2021 at 09:13 UTC
    Store the registers as keys in a hash of hashes, use the values as the inner keys. After the file has been processed, iterate over the registers and count how many keys the inner hashes have. Those with a single key are the constant registers.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %R; while (<<>>) { chomp; my ($register, $value) = split /,\s*/; ++$R{$register}{$value} } for my $register (keys %R) { say $register, "\t", keys %{ $R{$register} } > 1 ? 'variable' : 'constant'; }
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]