CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Appending to the end of a line from an input file

 



cupofdoug
Novice

Apr 14, 2013, 11:30 PM

Post #1 of 16 (789 views)
Appending to the end of a line from an input file Can't Post

Alright so I'm reading in line by line from a file. The lines from the input file can either be 2, 4, or 6 characters long. If they are 2 digits long I'm to add (append) 4 zeros to the end of the line, and if the line is 4 digits long I'm to add 2 0's, and if they're 6 they're good to go. I'm having a breakdown trying to figure out how to add these digits to the magic variable without a space to the recognized by the next part of my program (which coverts the 6 digits into Base64). Here's my code:

Code
while (defined ($_ = <SIXBITS>)) { 
chomp($_);

if (length() == 4){
$_ = "$_ 00";
print;
$onezeros = 1
}
if (length() == 2){
$_ = "$_ 0000";
$twozeros = 2
print;
}


The space inbetween the $_ and the zeros musn't be there, however if I remove it from the code I get yelled at. I know this is a rookie question but I'm stuck :[!

Thanks


Laurent_R
Enthusiast / Moderator

Apr 14, 2013, 11:41 PM

Post #2 of 16 (787 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

One possible way:


Code
$_ .= "00";



FishMonger
Veteran / Moderator

Apr 15, 2013, 7:57 AM

Post #3 of 16 (778 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post


Code
foreach my $num ( qw(22 4444 666666)) { 
my $zeros = ('0') x (6 - length($num));
say "$num $zeros";
}



(This post was edited by FishMonger on Apr 15, 2013, 7:58 AM)


Kenosis
User

Apr 15, 2013, 11:16 AM

Post #4 of 16 (765 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

Here's another option:


Code
use strict; 
use warnings;

while (<DATA>) {
$_ = ( sprintf '%6f', ".$_" ) * 1e6;
print "$_\n";
}

__DATA__
11
5555
999999
22
7777
444444


Output:


Code
110000 
555500
999999
220000
777700
444444



(This post was edited by Kenosis on Apr 15, 2013, 3:21 PM)


cupofdoug
Novice

Apr 15, 2013, 8:40 PM

Post #5 of 16 (745 views)
Re: [Kenosis] Appending to the end of a line from an input file [In reply to] Can't Post

thanks everyone, Laurent's way works nicely for what I'm doing.


gkol0279
New User

Apr 15, 2013, 9:43 PM

Post #6 of 16 (744 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

Thanks Laurent. It worked for me.


Laurent_R
Enthusiast / Moderator

Apr 16, 2013, 10:55 AM

Post #7 of 16 (734 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

Personally, if I had to solve the overall problem, I would probably use rather something like Kenosis or Fishmonger have suggested, but I tried in my answer to give a solution to the exact problem you were asking and with the minimal amount of changes compared to your code.


FishMonger
Veteran / Moderator

Apr 16, 2013, 11:15 AM

Post #8 of 16 (731 views)
Re: [Kenosis] Appending to the end of a line from an input file [In reply to] Can't Post

I'm not sure if anyone else noticed this, but Kenosis's approach failed to meet one of the key requirements.

Quoted from the OP's opening post.

Quote
The space inbetween the $_ and the zeros musn't be there, however if I remove it from the code I get yelled at.



Laurent_R
Enthusiast / Moderator

Apr 16, 2013, 11:58 AM

Post #9 of 16 (727 views)
Re: [FishMonger] Appending to the end of a line from an input file [In reply to] Can't Post

Then we don't understand the original requirement the same way.

My understanding is that the OP wants:


Code
110000  
555500


not:


Code
11 0000  
5555 00



FishMonger
Veteran / Moderator

Apr 16, 2013, 12:02 PM

Post #10 of 16 (726 views)
Re: [Laurent_R] Appending to the end of a line from an input file [In reply to] Can't Post

lol, you're right. I misread that line. I read must not musn't. That goes to prove that the eyes "see" one thing but the brain could "see" something else.


cupofdoug
Novice

Apr 16, 2013, 10:21 PM

Post #11 of 16 (717 views)
Re: [FishMonger] Appending to the end of a line from an input file [In reply to] Can't Post

Ah yes, the strings must all be uniformly 6 digits so they can be read in the next phase of the program 6 digits at a time.

Alas, I'm stuck again. Earlier in the program I converted plain text to its ASCII value, then ASCII to binary using the following code:

Code
my $chunk2; 
while (read(OUTFILE, $chunk2, 1)) {

my $asciiChar = ord $chunk2;
my $binary = sprintf("%08b", $asciiChar);
print OUT2 $binary;


I was given the "my $binary = sprintf("%08b", $asciiChar);" part, and I'm not sure of how it works to convert an ascii character to binary, and now I need to convert binary back to ascii (and then ascii back to plaintext, I'm assuming using the "chr" function). So basically I have to reverse this process. Thanks again for any suggestions.


(This post was edited by cupofdoug on Apr 16, 2013, 10:27 PM)


Laurent_R
Enthusiast / Moderator

Apr 17, 2013, 12:44 AM

Post #12 of 16 (713 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

Hi,

I think that:


Code
my $binary = sprintf("%08b", $asciiChar);


is probably not doing what you think. In this case, the spintf converts your number not into a binary number, but into a string containing a binary representation of your number, i.e. a string o characters containing only the 0 and 1 characters.

For example, if you do this:


Code
$binary = sprintf("%08b", 17);


$binary now contains the following string of characters: "00010001", which is probably not what you are looking for. So, you probably don't want to do that conversion, and I am not sure you need to do any conversion, since the ascii value returned by the ord function is a number.


cupofdoug
Novice

Apr 17, 2013, 6:26 AM

Post #13 of 16 (706 views)
Re: [Laurent_R] Appending to the end of a line from an input file [In reply to] Can't Post

A string representation of the binary actually is exactly what I needed for that situation (still don't know exactly how that sprintf works unfortunately). The project I'm working on calls for a provided text file to be encoded into Base64 formatting from plain text. The process I was told to follow involves first converting each character of the plaintext to its ASCII representation, then converting the ASCII representation to its binary representation, then reading in chunks of 6 characters at a time from the binary string and converting these 6 char chunks into their Base64 equivalent (not always evenly distributed into 6 bit chunks, which is why I needed to append 00's to the end of the string on occassion). Finally I was to print the Base64 "code" into a new file.

The next part of the project calls for a text file already encoded into Base64 to be decoded into plaintext, so I'm reversing the process. I can get the binary representation from the Base64 character using a pretty lengthy if/elsif statement, but converting the binary representation to its ASCII equivalent is where I'm stuck.


Laurent_R
Enthusiast / Moderator

Apr 17, 2013, 9:35 AM

Post #14 of 16 (700 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post

For coding in base64 use this CPAN module:


Code
use MIME::Base64;


For example, you could encode a whole file in base64 using the following one-liner:


Code
perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' your_file.txt


As for the sprintf function, when you do:


Code
$binary = sprintf("%08b", 17);


The first argument is a format string in which:
- % says that you are applying the format to the next argument (17 in this case)
- 08 says that you want the resulting string to be formated on 8 characters padded with 0 on the lext
- d that the next argument should be interpreted as a signed integer, in decimal.

For more information, see: http://perldoc.perl.org/functions/sprintf.html


Laurent_R
Enthusiast / Moderator

Apr 17, 2013, 10:19 AM

Post #15 of 16 (697 views)
Re: [cupofdoug] Appending to the end of a line from an input file [In reply to] Can't Post


In Reply To
I can get the binary representation from the Base64 character using a pretty lengthy if/elsif statement, but converting the binary representation to its ASCII equivalent is where I'm stuck.


Use the oct function after having prepended "0b" to your binary string.

Quote from perldoc:


Code
perldoc -d oct 

oct EXPR
oct Interprets EXPR as an octal string and returns the
corresponding value. (If EXPR happens to start off with "0x",
interprets it as a hex string. If EXPR starts off with "0b",
it is interpreted as a binary string. Leading whitespace is
ignored in all three cases.)



For example, try this:


Code
my $binary = "0b" . "010100"; 
print oct $binary; # will print 20



cupofdoug
Novice

Apr 18, 2013, 9:31 PM

Post #16 of 16 (681 views)
Re: [Laurent_R] Appending to the end of a line from an input file [In reply to] Can't Post


In Reply To
For example, try this:


Code
my $binary = "0b" . "010100"; 
print oct $binary; # will print 20



This is exactly what I needed, thank you so much!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives