Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Hash Depriciated

by Thresh (Initiate)
on Dec 04, 2013 at 20:58 UTC ( [id://1065655]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all;

I am not new to perl but I am no where near great.

I was wondering if you could help me revise this so I am not getting the Hash depreciation error?

The following code works, but I still receive an error.

#!/usr/bin/perl -w use strict; use IO::Socket::INET; $| = 1; my @bin_data; my @hex; my $count; my %h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011", 4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111", 8 => "1000", 9 => "1001", a => "1010", b => "1011", c => "1100", d => "1101", e => "1110", f => "1111", A => "1010", B => "1011", C => "1100", D => "1101", E => "1110", F => "1111", ); my $out = "12315141800F1F5F3F3F63410026DE8F45"; print "hex string = $out\n"; my $c=0; for ($count = 0; $count + 2 <= length($out); $count += 2) { $hex[$c] = substr($out,$count,2); $bin_data[$c] = %h2b->{substr($hex[$c],0,1)}.%h2b->{substr($he +x[$c],1,1)}; print "count=$c hex=$hex[$c] bin=$bin_data[$c]\n"; $c++; }

when i run it this is the output and error i receive.

Using a hash as a reference is deprecated at ./hexconvt.pl line 27. Using a hash as a reference is deprecated at ./hexconvt.pl line 27. hex string = 12315141800F1F5F3F3F63410026DE8F45 count=0 hex=12 bin=00010010 count=1 hex=31 bin=00110001 count=2 hex=51 bin=01010001 count=3 hex=41 bin=01000001 count=4 hex=80 bin=10000000 count=5 hex=0F bin=00001111 count=6 hex=1F bin=00011111 count=7 hex=5F bin=01011111 count=8 hex=3F bin=00111111 count=9 hex=3F bin=00111111 count=10 hex=63 bin=01100011 count=11 hex=41 bin=01000001 count=12 hex=00 bin=00000000 count=13 hex=26 bin=00100110 count=14 hex=DE bin=11011110 count=15 hex=8F bin=10001111 count=16 hex=45 bin=01000101

Any help would be greatly appreciated not depreciated.

Thanks in advance.

Replies are listed 'Best First'.
Re: Hash Depriciated
by tangent (Parson) on Dec 04, 2013 at 21:00 UTC
    Replace %h2b->{substr...} with $h2b{substr...}
Re: Hash Depriciated
by tobyink (Canon) on Dec 04, 2013 at 21:43 UTC

    tangent's answer is correct.

    I'll point out that depreciated and deprecated are different things. Depreciated means that something's monetary value has lowered over time. Deprecated in general English usage, means that something is strongly disapproved of; deprecated in the field of computer science refers to a feature which is considered obsolete and which may be dropped in the future.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      "Deprecated" was depreciated after "depreciated" was deprecated, but please don’t ask me why...

Re: Hash Depriciated
by thezip (Vicar) on Dec 04, 2013 at 22:10 UTC

    I offer this slightly more compact code:

    use strict; my $out = "12315141800F1F5F3F3F63410026DE8F45"; for my $hex ($out =~ /(\S{2})/g) { # split the strings into chunks of + two chars printf "hex => %s, binary => %08B\n", $hex, hex($hex); }
    __OUTPUT__ hex => 12, binary => 00010010 hex => 31, binary => 00110001 hex => 51, binary => 01010001 hex => 41, binary => 01000001 hex => 80, binary => 10000000 hex => 0F, binary => 00001111 hex => 1F, binary => 00011111 hex => 5F, binary => 01011111 hex => 3F, binary => 00111111 hex => 3F, binary => 00111111 hex => 63, binary => 01100011 hex => 41, binary => 01000001 hex => 00, binary => 00000000 hex => 26, binary => 00100110 hex => DE, binary => 11011110 hex => 8F, binary => 10001111 hex => 45, binary => 01000101

    *My* tenacity goes to eleven...

      Using a statement modifier also saves on typing.

      $ perl -Mstrict -Mwarnings -e ' my $out = q{12315141800F1F5F3F3F63410026DE8F45}; printf qq{hex => %s, binary => %08B\n}, $_, hex for unpack q{(a2)*}, $out;' hex => 12, binary => 00010010 hex => 31, binary => 00110001 hex => 51, binary => 01010001 hex => 41, binary => 01000001 hex => 80, binary => 10000000 hex => 0F, binary => 00001111 hex => 1F, binary => 00011111 hex => 5F, binary => 01011111 hex => 3F, binary => 00111111 hex => 3F, binary => 00111111 hex => 63, binary => 01100011 hex => 41, binary => 01000001 hex => 00, binary => 00000000 hex => 26, binary => 00100110 hex => DE, binary => 11011110 hex => 8F, binary => 10001111 hex => 45, binary => 01000101 $

      I hope this is of interest.

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1065655]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-16 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found