Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > convert integer to string

Reply
Thread Tools

convert integer to string

 
 
ccc31807
Guest
Posts: n/a
 
      06-08-2010
This is embarrassing!

I read an input file, split each row on the separators, pick out the
unique key (which consists of seven digits), create a hash element on
the key, and read the other values into an anonymous hash. This has
worked for years without a hitch.

Yesterday, my user requested the app to print a calculated field,
which is a list of names contained in a secondary file. I thought, "No
problem, I'll just create a new element in my anonymous hash,
concatenate each name unless the name already matched the string, and
print it out." Didn't work, but the errors seemed to be random and
arbitrary.

After a couple of hours, I used bdf's trick to print out the hash, and
discovered much to my embarrassment that the keys weren't the same. In
the main loop where I created the hash, the keys were as expected and
consisted of seven characters which are all digits. HOWEVER, in the
loop where I created the additional hash element, the keys with
leading zeros did not match. Here is an example of the hash:

0123456 => HASH(deadbeed)
id => 0123456
name => joe
gender => male
new_field =>
123456 => HASH(deadbeef)
new_field => list of concatenated strings

I read the main file and create the main hash on the unique key, one
element of which is 'new_field' with an initial value of ' '. Then, I
read a secondary file which contains the key and ATTEMPT to
concatenate an element to the main hash element new_field. Instead, it
creates a new hash element with the numeric value of the unique key
instead of the string value.

So, I guess my question is how I convert an integer to a string to
preserve the leading zeros.

According to MJD on his infrequently asked questions page, the answer
is, "Try using a whip." But that doesn't tell me what kind of whip.
http://perl.plover.com/IAQ/IAQlist.h...er_as_a_string

CC.
 
Reply With Quote
 
 
 
 
Ralph Malph
Guest
Posts: n/a
 
      06-08-2010
> So, I guess my question is how I convert an integer to a string to
> preserve the leading zeros.
>
> According to MJD on his infrequently asked questions page, the answer
> is, "Try using a whip." But that doesn't tell me what kind of whip.
> http://perl.plover.com/IAQ/IAQlist.h...er_as_a_string

The whip I usually use is string concatenation or variable interpolation
within double quotes.
Something like

my $number=5;
my $string="000".$number;

Surely there are several ways to do this.
This always works for me!

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      06-08-2010
>>>>> "c" == ccc31807 <> writes:

c> After a couple of hours, I used bdf's trick to print out the hash, and
c> discovered much to my embarrassment that the keys weren't the same. In
c> the main loop where I created the hash, the keys were as expected and
c> consisted of seven characters which are all digits. HOWEVER, in the
c> loop where I created the additional hash element, the keys with
c> leading zeros did not match. Here is an example of the hash:

what trick? use Data:umper and no trick is needed.

c> 0123456 => HASH(deadbeed)
c> id => 0123456

that is an OCTAL literal. when parsed into perl it will be converted to
an integer and later printed in decimal. if those are always keys which
means strings, never print or use them without quotes. don't think of
them as numbers but strings with all digits.

c> So, I guess my question is how I convert an integer to a string to
c> preserve the leading zeros.

if you already have corrupted numbers, use sprintf to pad them with
zeros. but i suspect you will still have nasty errors as you will have
converted from the literal octal value now to a decimal. you can sprintf
the number back in octal to compensate. the proper solution is to never
let perl see those as literal octal numbers but always as strings. i
dunno what your code is doing (as you didn't post any) to make this
happen. are you doing an eval on some incoming data? that is a no-no! do
a proper parse and you can keep those keys as strings.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      06-08-2010
On Jun 8, 2:26*pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> dunno what your code is doing (as you didn't post any) to make this
> happen. are you doing an eval on some incoming data? that is a no-no! do
> a proper parse and you can keep those keys as strings.


This is what I'm doing.

The main file looks like this:
0123456|joe|male|etc ...

which I manipulate as follows:
my ($id, $name, $gender, @rest) = split /\|/;
$main_hash{$id} = {
id => $id,
name => $name.
gender => $gender,
rest => @rest,
new_value => ' ',
};

The secondary file looks like this:
0123456|this|etc ...
0123456|is|etc ...
0123456|a|etc ...
0123456|list|etc ...
0123456|of|etc ...
0123456|strings|etc ...

which I manipulate as follows:
my ($id, $string, @rest) = split /\|/;
$main_hash{$id}{new_value} .= $string unless $main_hash{$id}
{new_value} =~ /$string/; #the strings can be duplicated but I only
want one of each

So, should I double quote the $id when I use it as a hash key? That
strikes me as idiosyncratic even for Perl.

CC.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      06-08-2010
ccc31807 wrote:
> On Jun 8, 2:26 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
>> dunno what your code is doing (as you didn't post any) to make this
>> happen. are you doing an eval on some incoming data? that is a no-no! do
>> a proper parse and you can keep those keys as strings.

>
> This is what I'm doing.
>
> The main file looks like this:
> 0123456|joe|male|etc ...
>
> which I manipulate as follows:
> my ($id, $name, $gender, @rest) = split /\|/;
> $main_hash{$id} = {
> id => $id,
> name => $name.
> gender => $gender,
> rest => @rest,
> new_value => ' ',
> };
>
> The secondary file looks like this:
> 0123456|this|etc ...
> 0123456|is|etc ...
> 0123456|a|etc ...
> 0123456|list|etc ...
> 0123456|of|etc ...
> 0123456|strings|etc ...
>
> which I manipulate as follows:
> my ($id, $string, @rest) = split /\|/;
> $main_hash{$id}{new_value} .= $string unless $main_hash{$id}
> {new_value} =~ /$string/; #the strings can be duplicated but I only
> want one of each
>
> So, should I double quote the $id when I use it as a hash key? That
> strikes me as idiosyncratic even for Perl.
>
> CC.


Provide actual code that we can run that shows your issue and so we can
see what's happening.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      06-08-2010
ccc31807 wrote:
> This is embarrassing!
>
> I read an input file, split each row on the separators, pick out the
> unique key (which consists of seven digits), create a hash element on
> the key, and read the other values into an anonymous hash. This has
> worked for years without a hitch.
>
> Yesterday, my user requested the app to print a calculated field,
> which is a list of names contained in a secondary file. I thought, "No
> problem, I'll just create a new element in my anonymous hash,
> concatenate each name unless the name already matched the string, and
> print it out." Didn't work, but the errors seemed to be random and
> arbitrary.
>
> After a couple of hours, I used bdf's trick to print out the hash, and
> discovered much to my embarrassment that the keys weren't the same. In
> the main loop where I created the hash, the keys were as expected and
> consisted of seven characters which are all digits. HOWEVER, in the
> loop where I created the additional hash element, the keys with
> leading zeros did not match. Here is an example of the hash:


Most folks really don't need to know the backstory. Post your code,
your results, your expectations, and your questions.
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      06-08-2010
On Jun 8, 3:18*pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> Provide actual code that we can run that shows your issue and so we can
> see what's happening.


Here is the working code in my test script. Unfortunately, I can't
post the data files. Note that this code does some other things and
contains debugging statements.

open FAC, '<', "FAC_${term}.csv", or die "Cannot open FAC, $!";
chomp ($header = <FAC>);
while (<FAC>)
{
next unless /\w/;
chomp;
my ($changed, $last, $first, $middle, $id2, $region, $contract,
$addy1, $addy2, $csz, $mail, $trs, @courses) = parse_line(',', $bool,
$_);
print "id2 is [$id2] and facid is [$facid]\n";
if ($facid !~ /\?/) { next unless $id2 eq $facid; }
$fac{$id2} = {
id2 => $id2,
contract => $contract,
first => $first,
middle => $middle,
last => $last,
addy1 => $addy1,
addy2 => $addy2,
csz => $csz,
mail => $mail,
trs => $trs,
courses => @courses,
xlist => '',
}
}
close FAC;

open SEC, '<', "$sections_file", or die "Cannot open SEC, $!";
chomp ($header = <SEC>);
while (<SEC>)
{
next unless /\w/;
chomp;
s/'/\\'/g;
my ($last, $first, $middle, $id1, $filename, $crs_id, $site, $loc,
$glcode, $level, $count, $status, $section, $title, $hours, $xlist,
$total, $travel, $contract) = parse_line(',', 0, $_);
next if $contract =~ /N/;
$sec{$crs_id} = {
crs_id => $crs_id,
filename => $filename,
id1 => $id1,
site => $site,
loc => $loc,
glcode => $glcode,
level => $level,
count => $count,
status => $status,
section => $section,
title => $title,
hours => $hours,
xlist => $xlist,
total => $total,
travel => $travel,
contract => $contract,
};
$xlist{$id1} .= "$section " if $xlist =~ /\d/ and $xlist !~ /
$section/;
$fac{$id1}{xlist} .= "$section " if $xlist =~ /\d/;
}
close SEC;



 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-08-2010
>>>>> "c" == ccc31807 <> writes:

c> On Jun 8, 2:26*pm, "Uri Guttman" <u...@StemSystems.com> wrote:
>> dunno what your code is doing (as you didn't post any) to make this
>> happen. are you doing an eval on some incoming data? that is a no-no! do
>> a proper parse and you can keep those keys as strings.


c> This is what I'm doing.

c> The main file looks like this:
c> 0123456|joe|male|etc ...

c> which I manipulate as follows:
c> my ($id, $name, $gender, @rest) = split /\|/;
c> $main_hash{$id} = {
c> id => $id,
c> name => $name.
c> gender => $gender,
c> rest => @rest,

that is wrong as it will put the whole array there. you need a ref to
that array or an anon ref.

c> new_value => ' ',
c> };

c> The secondary file looks like this:
c> 0123456|this|etc ...
c> 0123456|is|etc ...
c> 0123456|a|etc ...
c> 0123456|list|etc ...
c> 0123456|of|etc ...
c> 0123456|strings|etc ...

c> which I manipulate as follows:
c> my ($id, $string, @rest) = split /\|/;
c> $main_hash{$id}{new_value} .= $string unless $main_hash{$id}
c> {new_value} =~ /$string/; #the strings can be duplicated but I only
c> want one of each

c> So, should I double quote the $id when I use it as a hash key? That
c> strikes me as idiosyncratic even for Perl.

in that limited code i don't see where the keys would be interpreted as
literal numbers. there must be something else going on which is doing
that. perl won't lose leading zeroes in strings without doing some
number conversions. are you sure you never look at those kays as
numbers? like use == to check them or similar? since they seem to be
fixed size you can always use the string comparison ops safely.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-08-2010
>>>>> "c" == ccc31807 <> writes:

c> On Jun 8, 3:18*pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
c> wrote:
>> Provide actual code that we can run that shows your issue and so we can
>> see what's happening.


c> Here is the working code in my test script. Unfortunately, I can't
c> post the data files. Note that this code does some other things and
c> contains debugging statements.

c> open FAC, '<', "FAC_${term}.csv", or die "Cannot open FAC, $!";
c> chomp ($header = <FAC>);
c> while (<FAC>)
c> {
c> next unless /\w/;
c> chomp;
c> my ($changed, $last, $first, $middle, $id2, $region, $contract,
c> $addy1, $addy2, $csz, $mail, $trs, @courses) = parse_line(',', $bool,
c> $_);
c> print "id2 is [$id2] and facid is [$facid]\n";
c> if ($facid !~ /\?/) { next unless $id2 eq $facid; }
c> $fac{$id2} = {
c> id2 => $id2,
c> contract => $contract,
c> first => $first,
c> middle => $middle,
c> last => $last,
c> addy1 => $addy1,
c> addy2 => $addy2,
c> csz => $csz,
c> mail => $mail,
c> trs => $trs,
c> courses => @courses,

same bug as i pointed out in another post. you need a ref or anon array
there. that is very wrong. who knows what it is doing to your app?

c> xlist => '',
c> }

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      06-08-2010
On Jun 8, 3:06*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> '%main_hash' is an appallingly bad name for a variable. Why is it there?
> What's it got in it? (In this case, probably, something like '%people'
> might be better.)



Sorry. I posted the code where I populated the two hashes, named %fac
and %sec.

> You want \Q\E here.


Trying that now.

> So you must be doing something else.


Not intentionally.

CC.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split string (then) Convert string into Integer news ASP General 2 05-26-2010 11:58 AM
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Convert a Base32 string to Base10 integer Jon Paskett ASP .Net 1 12-18-2005 01:31 AM
convert scientific integer to normal integer les ander Python 4 10-05-2004 04:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57