Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

file conversion in perl

Posted on 2009-02-23
2
Medium Priority
?
208 Views
Last Modified: 2012-05-06
I am taking an input file with a list of names. then for every occurence of Clark Kent, I am switching it with SUPERMAN!!!. I have some simple code below. where am i going wrong?
#!/usr/bin/perl -w
 
open("name.txt", "<filename.in");
open("nameout.txt", ">filename.out");
while(<name.txt>)
{
        my($line) = $_;
        chomp($line);
        if($name = Clark Kent)
        {
                $newnme = "SUPERMAN!!!";
                my($first, $last) = split(//, $name);
                $line = sprintf($name, $newnme );
        }
}
close("name.txt");
close("nameout.txt");

Open in new window

0
Comment
Question by:swinfosec
2 Comments
 
LVL 1

Expert Comment

by:benofvan
ID: 23716300
One thing I see is that you cannot compare a string with "=", and the string must be quoted.  it must be like:

if ($name eq "Clark Kent") {
}
0
 
LVL 48

Accepted Solution

by:
Tintin earned 1000 total points
ID: 23717981
You have a lot of code you don't need.

You could do it as a one liner, ie:

perl -pe 's/Clark Kent/Superman!!!/' filename.in >filename.out

or use the code below
#!/usr/bin/perl
use strict;
use warnings;
 
open NAME, "filename.in" or die "Can not open filename.in $!\n";
open NAMEOUT, ">filename.out" or die "Can not open filename.out $!\n";
 
while(<NAME>) {
  s/Clark Kent/SUPERMAN!!!/;
  print NAMEOUT;
}

Open in new window

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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
Suggested Courses

810 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