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

oct and vec

by pysome (Scribe)
on Aug 07, 2007 at 08:11 UTC ( [id://630973]=perlquestion: print w/replies, xml ) Need Help??

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

vec($foo,0,8) == ord($foo) Right? Can anyone explain some vec function for me?Thanks. I have readed the document in perldoc,but so puzzled!

Replies are listed 'Best First'.
Re: oct and vec
by Zaxo (Archbishop) on Aug 07, 2007 at 08:50 UTC

    The first argument of vec says what variable it acts on. The third says how many bits make a unit in the vector. The second is the offset in units of the third argument where things will happen.

    ord applied to a string returns ord of its first character.

    Thus, vec($foo,0,8) = ord($foo); is a no-op, replacing the first character of $foo by itself. In more depth:

    $ perl -e'my $foo = "qa"; vec($foo,0,8) = ord $foo;print $foo,$/'
    qa
    $ perl -e'my $foo = "qa"; vec($foo,1,8) = ord $foo;print $foo,$/'
    qq
    $ perl -e'my $foo = "qa"; vec($foo,2,8) = ord $foo;print $foo,$/'
    qaq
    
    Further increments of the second argument would place NUL in the intermediate positions of the string.

    Update: I just noticed that the op is ==, numerical equality. I think that should always be true, but there might be tricky edge cases related to encoding.

    After Compline,
    Zaxo

Re: oct and vec
by citromatik (Curate) on Aug 07, 2007 at 09:14 UTC

    This is an example of a bit vector (a vector of "0"s or "1"s)

    ## Define a bit vector with positions 4 to 7 set to 1 my $bv1=''; vec($bv1,$_,1)=1 foreach (3..6); dumpbv($bv1); sub dumpbv { my $bitv = shift @_; my $pos=0; foreach (split "",unpack("b*",$bitv)){ print; $pos++; } }

    With bit vectors you can do bit operations. for example, to find the overlapping ("OR") of 2 bit vectors:

    sub sumbv { return $_[0] | $_[1]; }

    ... and the AND operation:

    sub cmpbv { return $_[0] & $_[1]; }

    A full working dummy example with these functions would be:

    use strict; use warnings; ## Define a bit vector with positions 4 to 7 set to 1 my $bv1=''; vec($bv1,$_,1)=1 foreach (3..6); dumpbv ($bv1); print " bv1\n"; # Define a bit vector with positions 3 to 5 set to 1 my $bv2=''; vec($bv2,$_,1)=1 foreach (2..4); dumpbv ($bv2); print " bv2\n"; print "--------\n"; my $bv_sum = sumbv($bv1,$bv2); dumpbv ($bv_sum);print " OR\n"; my $bv_cmp = cmpbv($bv1,$bv2); dumpbv ($bv_cmp);print " AND\n"; sub sumbv { return $_[0] | $_[1]; } sub cmpbv { return $_[0] & $_[1]; } sub dumpbv { my $bitv = shift @_; my $pos=0; foreach (split "",unpack("b*",$bitv)){ print; $pos++; } }

    Outputs:

    00011110 bv1 00111000 bv2 -------- 00111110 OR 00011000 AND

    Hope this helps

    citromatik

      How are any of these code examples useful and or how are you applying these in a work environment?
        The ability to manipulate bits is essential in many low level tasks and in some memory-efficient algorithms.
Re: oct and vec
by ikegami (Patriarch) on Aug 07, 2007 at 17:23 UTC

    I can't think of any time where the two would be different when $foo contains a string of octets, but the equality is not always true if $foo contains a string of chars.

    >perl -MHTML::Entities -le"$foo=decode_entities('♥'); print for + vec($foo,0,8), ord($foo)" 226 9829

    vec($f,0,8) looks at the first octet of the internal representation of the string, whereas ord looks at the first character of the string.

Re: oct and vec
by pysome (Scribe) on Aug 07, 2007 at 09:37 UTC
    Great help.Thx everybody!!!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-25 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found