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

Greetings Monks; My question is concerning the following code: How do I populate the hash with the token (letter or number)as a key and its binary representation as the value?

$file=pop @ARGV; chomp $file; open (MESG, "+<" ,"$file")|| die "Phile unavailable:$!\n"; while(){ $character=$_; @letters=split(/\n/ ,$character); foreach $letter(@letters){ $ascii=unpack ("C*" ,$letter); $bin=unpack ("b*",$letter); $ordinal=ord $letter; print "\n1)$letter\n"; print "2)$ordinal\n"; print "3)$ascii\n"; print "4)$bin\n"; push @bin ,$bin; } } my %letnet; foreach $letter(@letters){ foreach $binary(@bin){ $letnet->{$letter}=$binary; } } while (($key,$value)=each (%letnet)){ print "$key-->$value\n"; }

janitored by ybiC: format codeblock for legibility

Replies are listed 'Best First'.
Re: Populating A Hash
by Zaxo (Archbishop) on Mar 02, 2005 at 02:14 UTC

    You would be helped by a code indentation convention. I've made some corrections and rigged it to pass strictures.

    use warnings; use strict; my $file = pop @ARGV; chomp $file; my (@letters, @bin); # added open MESG, '+<' , $file or die $file, ' unavailable: ', $!; while (<MESG>) { # changed to read from the open handle my @characters = split //; push @letters, @characters; push @bin, map { my $c = unpack 'b*', $_ } @characters; # foreach my $letter (@letters) { # my ($ascii) = unpack ("C" ,$letter); # my ($bin) = unpack ("b*",$letter); # my $ordinal = ord $letter; # print "\n1)$letter\n"; # print "2)$ordinal\n"; # print "3)$ascii\n"; # print "4)$bin\n"; # # push is moved above # } } my %letnet; @letnet{@letters} = @bin; # slice replaces the double foreach while (my ($key,$value) = each (%letnet)) { print "$key-->$value\n"; }

    Your doubly nested hash populating loop was wrong. For each letter, it churns through all the binary representations, assigning each in turn to the $letter key's value. Eventually each key gets the value $bin[-1]. I replaced it with the efficient hash slice mechanism.

    Why do you use the '+<' signature in open? It is unnecessary to have the file open to write in what you show. If you later write to the file you should call flock after open.

    After Compline,
    Zaxo

      If you later write to the file you should call flock after open.

      That doesn't resolve the race condition. Flock before open is the way to go.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        Where do you get the filehandle to call flock by?

        After Compline,
        Zaxo

        No, flock after opening is correct I belive. You might be thinking of the common error of opening a file in ">" mode, which O_TRUNCates the file, thus changing it before you had a chance to flock it.

Re: Populating A Hash
by chas (Priest) on Mar 02, 2005 at 02:11 UTC
    One comment: In your first while loop, do you mean while(<MESG>) rather than while()? (As it stands, it doesn't seem like it can do anything.) With that change it does run (but I haven't studied it enough to really understand what the point is.) You could explain what you're trying to do if you wish.
    chas
      Yes the intention was to include the file handle,just a typo unfortuately.My main inquiry was to learn how to populate a hash. Thanks for the help.