Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Files ,working with numers in Hex-Dec

Posted on 2006-06-25
3
Medium Priority
?
190 Views
Last Modified: 2008-03-06
Hi ,plese help me to solve the problem:

I have a file with content

Adress1    Adress2      ColumnA      ColumnB
----------- ------------   -------------- -------------
010FAC3  2345ABC     Text1           Text2
010AAB3  1345ABC     Text3           Text4
010BA13  1234567      Text5           Text6
.
.
.

Adress1 and Adress2 are some memory adreseses in HEX

I need a script in perl that read this file and add to the file column (Adress2-Adress1)in decimal and if it's not difficult sorted by this new column

Output file should be :
Adress1    Adress2     NewColumn(Adress2-Adress1 in decimal) ColumnA      ColumnB
010BA13  1234567      17992532                                               Text5           Text6
010AAB3  1345ABC     19116041                                               Text3           Text4
010FAC3  2345ABC     35872761                                               Text1           Text2

Please can somebody help me in solving this problem

Thanks,
Inga

0
Comment
Question by:Inga_dusha
3 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 600 total points
ID: 16980450
my @x;
while( <> ){
    next unless /^[0-9a-f]+\s+[0-9a-f]+/i;
    push @x,[split];
}
splice @$_,2,0,(hex $_->[1])-hex $_->[0] for @x;
$"="\t";
print "@$_\n" for sort{$a->[2]<=>$b->[2]} @x;
0
 

Author Comment

by:Inga_dusha
ID: 16981675
ozo,please can you explain me ,where I am reading the file here ?

It's should be something like this or what ?

open(file1, "<file.txt") || die("Could not open result file");
while( $record = <file1> ){
    next unless /^[0-9a-f]+\s+[0-9a-f]+/i;
    push @x,[split];
}

Thanks for help
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16982814
ozo's suggestion reads STDIN
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question