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:
Perl to create a Column

 



Pshields1984
New User

Jul 10, 2017, 11:02 AM

Post #1 of 4 (708 views)
Perl to create a Column Can't Post

Hi folks,

I am new(ish) to Perl. I have experience using Bash and Awk. I do love Awk. However I am in an environment where Perl is my weapon of choice.

My request is basic enough but I am completely unsure where to start. I need Perl to perform some tasks that Awk can.

I have a file containing

Column A
12345.pdf
12346.pdf
12347.pdf
12348.pdf
12547.pdf
14758.pdt


What I need is another column based on there being anything in Column A. So I need Perl to print a specific string in Column B if anything exists in Column A.


Example output would be as follows. Any help greatly appreciated and thank you.


Column A Column B
12345.pdf This Works
12346.pdf This Works
12347.pdf This Works
12348.pdf This Works
12547.pdf This Works
14758.pdt This Works


BillKSmith
Veteran

Jul 10, 2017, 1:20 PM

Post #2 of 4 (701 views)
Re: [Pshields1984] Perl to create a Column [In reply to] Can't Post

I am sorry that I cannot offer more help with the information you have provided. It is not clear if the text "Column A" is part of your input file or if it is annotation for your post. Is the text an actual dump of the input file or is it one column from a multi-column file? Your example does not include a single record that does not require the new text. I cannot tell what separates columns. It appears to be whitespace, but then your output would have three columns rather than two (A and B). If the column separator is a TAB, I recommend that you change it to a (:) or some other printable character (at least in your post).

You indicate that you want a perl program that does the same thing as an existing AWK program. It would be a big help if you show us the AWK code.

I look forward to seeing your clarifications.

Perl has several command-line switches which make it act much like AWK. I recommend that you avoid these until you learn to solve the problem with more normal perl. You ask where to start. Begin EVERY perl program with the commands

Code
use strict; 
use warnings;


This will require you to declare every variable with 'my'. That is a small price to pay for the debug time you will save.
Open your input file for input and your output file for output. (Use the three argument form of open).

Code
open my $input_handle, '<', input.csv;

Use a while loop to read through the input file.
Modify each record and print it to the output file inside the loop. When the loop ends, close both files.
Good Luck,
Bill


Pshields1984
New User

Jul 10, 2017, 3:21 PM

Post #3 of 4 (695 views)
Re: [BillKSmith] Perl to create a Column [In reply to] Can't Post

Hi Bill,

You are right, reading back on my question and it makes little sense. I'll clarify.

What I am using
OS Windows 7
Perl Strawberry

I'm getting my data by the using following command.

cd my\directory\filenames\
dir /b > listoffilenames.txt

listfilenames.txt as an example would contain.

1234.pdf
1245.pdf
1678.pdf

I need Perl to create a tab delimited file with a list of the filenames from listfilenames.txt and in the column beside those filenames I want that column to say something like "Send This File".

So my desired output should look like this ((\t) is to show that I want it tab delimited)

1234.pdf (\t) Send This File
1245.pdf (\t) Send This File
1678.pdf (\t) Send This File

I have done it with Awk but I haven't got access to my Linux machine at the moment to get the exact code.

Any help is greatly appreciated.

Thanks.


(This post was edited by Pshields1984 on Jul 10, 2017, 3:22 PM)


FishMonger
Veteran / Moderator

Jul 10, 2017, 4:39 PM

Post #4 of 4 (687 views)
Re: [Pshields1984] Perl to create a Column [In reply to] Can't Post

There's no need use the dir command to create the file listing. Perl has multiple easy ways to get that list. There also are multiple ways in Perl to create the new file listing.

Here is one method using your existing text file, but it's not really how I'd do it myself.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

my $filelist = 'listoffilenames.txt';
my $newlist = 'new_filelist.txt';

open my $in_fh, '<', $filelist;
open my $out_fh, '>', $newlist;

while (my $filename = <$in_fh>) {
chomp $filename;
say $out_fh "$filename\tSend This File";
}

close $in_fh;
close $out_fh;


I personally prefer to use full scripts with proper error checking/handling but you could do this as a one-liner just like awk or sed.


(This post was edited by FishMonger on Jul 10, 2017, 4:43 PM)

 
 


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

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