Click to See Complete Forum and Search --> : string concatenation in perl
threadhead
04-16-2003, 09:51 PM
hi!!
i want to check if a given length of a string matches 4 without remainder.
if 1 or 3 remain i want to add a leading zero to the string.
if($size % 4 == 1 || 3)
{
"0" . "$binhex";
print "$binhex";
}
elsif($size % 4 == 2)
{
"00" . "$binhex";
}
that doesnt work because "0" doesnt exist in perl i think....
adding two zeros doesnt work either.
the interpreter tells me something like
useless use of concatenation.
uhm that hurts :confused:
how could i get this working?
thanks
karthik
04-16-2003, 10:11 PM
You do not need the double quotes ( " ) surrounding $binhex, try "0" . $binhex , it shoud do the trick
rid3r
04-16-2003, 10:25 PM
try this:
if(($size % 4) == ( 1 || 3 )) #possible logical error
{
$binhex .= "0";
print "$binhex";
}
elsif(($size % 4) == 2)
{
$binhex .= "00";
print "$binhex";
}
stumbles
04-16-2003, 10:29 PM
Originally posted by threadhead
hi!!
i want to check if a given length of a string matches 4 without remainder.
if 1 or 3 remain i want to add a leading zero to the string.
if($size % 4 == 1 || 3)
{
"0" . "$binhex";
print "$binhex";
}
elsif($size % 4 == 2)
{
"00" . "$binhex";
}
that doesnt work because "0" doesnt exist in perl i think....
adding two zeros doesnt work either.
the interpreter tells me something like
useless use of concatenation.
uhm that hurts :confused:
how could i get this working?
thanks
Whadya know, I just got my Learning Perl book today. So feel free to ignore anything I say.
Your right, "0" doesn't exist in Perl. However what about using the "undef" value? If you create a variable say, $string and not define it, it starts out empty. Maybe you could work with that?
scinerd
04-16-2003, 10:29 PM
this works you can change the size to test it.
#!/usr/bin/perl
use strict;
use warnings;
my $size = 6;
my $binhex = "string";
my $rd = $size%4;
if($rd == (1 || 3))
{
$binhex = "0$binhex";
print "$binhex \n";
}
elsif($rd == 2)
{
$binhex = "00$binhex";
print "$binhex \n";
}
rid3r
04-16-2003, 10:30 PM
or
$binhex = "0". "$binhex" ; # if the "0" goes in front
threadhead
04-17-2003, 04:49 AM
thanks for all your replies!
scinerd i tried your solution, it works. thanks ;)
rid3r doesnt that append the zeros to the end of the string?
cya