Reputation: 375
I have a variable, $count
, which will take a decimal value from the command line, and I have a piece of code which generates files with name filename001 filename002 ....filename00a,filename00b
etc. It appends a hexadecimal number(without 0x) to filename
for every new file it generates.
I use the variable $count
to keep track of the number that is appended to filename
. I want to pull out lines containing a particular text Result:
from the generated file (filename00b filename00a
, etc.). For this purpose I use the grep tool in the following way:
`grep "Result:" path to the file/filename0*$count`
This works fine till I reach the 10th file when 10 becomes a
in hexadecimal, but $count
in the grep command is simplified to 10, so grep is not able to find the file. I tried using hex($count)
, but it does not seem to work. It required that $count
variable can be incremented or decremented, and it still holds the hex value. Is there a way to do this?
Upvotes: 2
Views: 18174
Reputation: 124648
The hex
function in perl
interprets its argument as a hex string and returns the corresponding integer value, for example:
print hex '0xAf'; # prints '175'
print hex 'aF'; # same
This from the definition of hex
in perldoc -f hex
.
It seem you want to do the opposite, convert your decimal number to a hex representation. You can do that with printf
for example:
printf("%03x", 175); # prints '0af'
printf("%03x", 0xAf); # same
If you want to save the value in a variable, use sprintf
instead of printf
, it returns the string instead of printing it.
Upvotes: 8