in reply to Re: SQLServer varbinary headache
in thread SQLServer varbinary headache

That is more elegant than the code I wrote to try this out. Thanks for the swift and accurate advice.

I am not sure that I understand the differences in the different pack/unpack codes. What I tried before I got your answer, which also works, was:

while (@row = $sth->fetchrow_array()) { @s = unpack('C*',$row[4]); $sid = "0x"; foreach $s (@s) { $sid .= sprintf "%2.2x", $s; } $row[4] = $sid; #print $server,':',$db,':',join(':',@row),"\n"; $sth_ins->execute($server,$db,@row); }

What would the difference be between my code and yours?

Ed

Replies are listed 'Best First'.
Re: Re: Re: SQLServer varbinary headache
by fizbin (Chaplain) on Apr 24, 2004 at 00:43 UTC

    TMTOWTDI

    Well, the main difference is that I sequestered the convert-blob-to-long-hex-string dance off in its own subroutine (bin2hex) and then applied it to those columns that needed converting (just one column this time, but there might be other columns in similar code in the future, so you could reuse the code).

    As far as the way we both did the blob-to-hex-string conversion, there are some things that I think your version does better and some that mine does better. Basically, I'm not too comfortable with pack/unpack, so I tend to use it less than I really should. I am, however, very comfortable with map and so will use that in preference to a for loop when I feel it makes sense. (I still think that the corrected bin2hex I posted, based on what Util said in this same thread, is really the best option)

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      Right you are!

      I, on the other hand, have no clue how map works no matter how many times I read through its description. So, I tend to use the code from the previous programming incarnations I know (C, etc.). I will definitely use your subroutine in my next rev. as it is definitely more readable and maintainable.

      Ed