Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier).

My text document for example would be something as simple as

1312
4646
4231
3566
1341
2331

I know how to use sprintf, but I don't know how to make the function convert an entire file. I can only get it to convert one number that I input by myself. Does anyone know how to do this? Any help would be appreciated

You will have to read the file line by line and convert it.
For a start, this is how you read file line by line in perl

#open a file for writing the new vlues
open OUTFILE, ">your_new_file_name" or die "Error : Could not open [$!]";

my $readLine = "";
open FILE, "your_file_name" or die "Error : Could not open [$!]";
while (<FILE>) 
{   
  chomp;
  $readLine=$_;
  print $readLine, "\n";
  # Do you conversion here and write into another file
   print OUTFILE "Converted Value";
}


regards,
Ahamed

Hi, I am sorry I am very new at perl. Can you please show me how to write the converted number to another file? Also, if I keep writing to the second file one number at a time, will it overwrite anything? I want to make a new file that has several converted numbers

Thank you

---------- Post updated at 02:32 PM ---------- Previous update was at 02:15 PM ----------

This is what I have so far, but all I'm getting back is the name of the file dec.txt. I've created a file named dec.txt and hex.txt for this script.

 
#!/usr/bin/perl
#open a file for writing the new vlues
open OUTFILE, ">hex.txt" or die "Error : Could not open [$!]";
my $readLine = "";
open FILE, "dec.txt" or die "Error : Could not open [$!]";
while (<dec.txt>)
{
  chomp;
  $readLine=$_;
  print $readLine, "\n";
  # Do you conversion here and write into another file
  $hexval = sprintf("%x", $readline);
  open FILE, ">hex.txt" or die $!;
  print FILE $hexval;
  close FILE;
  print OUTFILE "Converted Value";
}

The first thing I notice is that your variable isn't being called correctly.

 
$hexval = sprintf("%x", $readline);

Change to

$hexval = sprintf("%x", $readLine);

I hate perl, but it looks like your conversion is okay except for that. Change that variable name and see if anything comes out. Let me know what output you're getting to hex.txt after the change if you're still having issues.

I actually noticed that same error and fixed it but got the same result. I am getting just a 0 as an output. Everytime I rerun the file, it adds another 0.

Try this

#!/usr/bin/perl

my $readLine = "";
#open a file for writing the converted values
open OUTFILE, ">hex.txt" or die "Error : Could not open [$!]";
#open the file to read the dec values
open INFILE, "dec.txt" or die "Error : Could not open [$!]";
while (<INFILE>)
{
  chomp;
  $readLine=$_;
  # Do you conversion here and write into another file
  $hexval = sprintf("%x", $readLine);
  print OUTFILE $hexval,"\n";
}
close INFILE;
close OUTFILE;

check your code, the variable name is $readLine not $readline. Alos made some changes to the file operation.
cheers!

regards,
Ahamed

One liner.

perl -nle 'BEGIN{open(OUT,">","hex.txt") or die "$!";} print OUT sprintf("%x",$_);END{close OUT;}' dec.txt