Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

example 7.pl

by Raymond (Novice)
on Aug 05, 2013 at 20:47 UTC ( #1047987=perlquestion: print w/ replies, xml ) Need Help??
Raymond has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; #extract column with unpack $a = "To be or not to be"; $b = unpack("x6 A6", $a); #skip 6, grab 6 print $b; ($b, $c) = unpack("x6 A2 x5 A2", $a); #forward 6, grab 2; backward 5 +, grab 2 print "$b\n$c\n";

7.pl
errors:

Global symbol "$c" requires explicit package name at 7.pl line 10.

Global symbol "$c" requires explicit package name at 7.pl line 11.

Execution of 7.pl aborted due to compilation errors.
Ok i will use diagnostics, thank you; this examples are from a book, there is more than one, and don't know if the book has a resolution or if it's mistaken...

Comment on example 7.pl
Download Code
Re: example 7.pl
by choroba (Abbot) on Aug 05, 2013 at 20:52 UTC
    Your post does not contain any question. You are using strict but not declaring your variables, which is the reason of the errors. $a and $b do not have to be declared because they are special - see perlvar.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      $a and $b do not have to be declared because they are special

      Which means that they should probably avoided for purposes other than what they are designed and earmarked for (basically sorting operations).

      @Raymond: if you *really* need to use single letter variables, start off with $c, avoid $a and $b. But don't you think it would be better to give meaningful names to your variables? I hold the opinion that giving meaningful names to variables, subroutines, filehandles, etc., is the #1 way of correctly commenting my code. Therefore, asides from quick tests under the debugger, I almost never use one-letter variable, with just one exception: loop variables, i.e. when the variable does not have any other meaning than looping through the subscripts of an array, where I find variable names such as $i, $j and $k to be acceptable. But, whereas using such loop variable is quite common in a language like C (where I come from), it is much less often useful in Perl, in which we can most of the time use better looping constructs (which usually loop directly over the elements, rather than the subscripts, of an array, thereby making meaningful names (the content of the array elements) again possible.

Re: example 7.pl
by toolic (Chancellor) on Aug 05, 2013 at 20:59 UTC
Re: example 7.pl
by AnomalousMonk (Abbot) on Aug 06, 2013 at 13:18 UTC
    ($b, $c) = unpack("x6 A2 x5 A2", $a);   #forward 6, grab 2; backward 5, grab 2

    And just to avoid another blind alley, the unpack template specifier for moving backward in a string is  'X' (upper case), not  'x' (lower case). See pack for a complete specification of pack/unpack template specifiers; also see the pack tutorial perlpacktut.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-26 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls