Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving print data to a variable

Status
Not open for further replies.
Jun 3, 2007
84
0
0
US
Hello,

Can't figure out how to save something which I am currently printing to a variable.

I have to captures ()() which I am converting data in the second capture from hex

Thanks for the help.

Example:

Code:
while ( $string =~ /(capture1)(capture2)/g ) {
    print "$1".chr(hex($2));
    #Trying to save that data to a variable
    #$capturedData = "$1".chr(hex($2)); #This does not work, can't figure out how to save the converted data to a var instead of printing
}
 
Did you read through the two tutorials I mentioned in your post thread219-1586552?
 
Oops... my mistake, that wasn't your post. But you should go read through the two tutorials mentioned there. :)
 
I can't see any problem with the code you posted, seems to work fine for me!

Code:
$ cat learningperl01
$string = 'foo41barfoo45';

while ( $string =~ /(foo)([[:xdigit:]]{2})/g ) {
    print "$1".chr(hex($2)) . "\n";
    $capturedData = "$1".chr(hex($2));
}

print "captured: $capturedData\n";
$ perl learningperl01
fooA
fooE
captured: fooE
$

Note that it will only capture the last data matched (i.e. last iteration of the while loop) unless you append it to a variable instead, or something...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top