Sign in to follow this  
Followers 0
darkie

Perl Help

15 posts in this topic

Wasn't sure where else to post this so I just decided to post it here. I used to have a .pl script that would run in windows activeperl that would basically take a text file and remove a certain number of characters (in my case 10) from the end of each line. I've lost the script and was wondering if anyone could write something like that real fast and just attach it to this thread. As I remember the script wasn't very complicated, but my Perl skills aren't up to par. Any help please?

0

Share this post


Link to post
Share on other sites

It's not perl, but it will compile and run on windows. Hmm, for some reason the forum is stripping the null termination so at like line[len-chars-1] = '\'; add a 0 after the \.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printHelp(char *file);

int main(int argc, char *argv[])
{
char file[100];
int chars = 10;
FILE *fp;
char line[80];
int len;

if (argc == 2)
{
strncpy(file, argv[1], sizeof(file));
} else if (argc == 3)
{
strncpy(file, argv[1], sizeof(file));
chars = atoi(argv[2]);
} else {
printHelp(argv[0]);
}

if ((fp = fopen(file, "r")) == NULL)
{
fprintf(stderr, "Unable to open: %s.", file);
exit(1);
}

while (fgets(line, sizeof(line), fp) != NULL)
{
if ((len = strlen(line)) >= chars)
{
line[len-chars-1] = '\';
printf("%s\n", line);
} else {
printf("\n");
}
}

fclose(fp);
return 0;
}

void printHelp(char *file)
{
printf("%s <file> [chars]\n", file);
exit(1);
}

Edited by livinded
0

Share this post


Link to post
Share on other sites

OK. First, I am a total perl newb. So please don't flame me.

The basics of this will be something like this.

my $var = 'abcdefghijklmnopqrstuvwxyz';

my $strlng = length($var) - 10;

my $output = substr($var, 0, $strlng);

print "$output \n";

I have tested this and it does work but I am unsure on how to use an external file with this. I am researching it now. Thanks for the little challenge.

0

Share this post


Link to post
Share on other sites

Got the first part working. No I need to learn how to turn the file into an array of lines. Shouldn't be long now.

0

Share this post


Link to post
Share on other sites
Got the first part working. No I need to learn how to turn the file into an array of lines. Shouldn't be long now.

open FILE, "pie.txt";

@rray = <FILE>;

0

Share this post


Link to post
Share on other sites

Ok. Final. Learned all of this in about 30 minutes. :)

my @var = <>;
for $line (@var) {
my $strlng = length($line) - 11;
my $output = substr($line, 0, $strlng);
print "$output \n";
}

usage: last10.pl filename

0

Share this post


Link to post
Share on other sites
[ bleeber@binrev.com ] perl $ cat testing 
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
[ bleeber@binrev.com ] perl $ ./last10.pl testing
abcdefghijklmnop
abcdefghijklmnop
abcdefghijklmnop
abcdefghijklmnop
abcdefghijklmnop
abcdefghijklmnop
abcdefghijklmnop
[ bleeber@binrev.com ] perl $

0

Share this post


Link to post
Share on other sites
open FILE, "pie.txt";

@rray = <FILE>;

I saw this but I wanted to take the input from the command line. I am really starting to like perl.

0

Share this post


Link to post
Share on other sites

That's blasphemy. The last thing the world needs is more perl programmers.

0

Share this post


Link to post
Share on other sites
Ok. Final. Learned all of this in about 30 minutes. :)

my @var = <>;
for $line (@var) {
my $strlng = length($line) - 11;
my $output = substr($line, 0, $strlng);
print "$output \n";
}

usage: last10.pl filename

this works, but i want it to save it to the text file instead of just outputting it into the command prompt screen. anyway to do that? thanks for the help.

0

Share this post


Link to post
Share on other sites

Just run the command like this:

last10.pl filename >> outputfile

the >> will redirect STDOUT to the file "outputfile"

Edited by bleeber
0

Share this post


Link to post
Share on other sites
That's blasphemy. The last thing the world needs is more perl programmers.

I want to learn some c as well. But there are a ton of c programmers too.

0

Share this post


Link to post
Share on other sites
Just run the command like this:

last10.pl filename >> outputfile

the >> will redirect STDOUT to the file "outputfile"

ok, thanks a ton!

0

Share this post


Link to post
Share on other sites
That's blasphemy. The last thing the world needs is more perl programmers.

/me cowers in the corner.

But come close and I'll zap you with my perl-fu

0

Share this post


Link to post
Share on other sites
Just run the command like this:

last10.pl filename >> outputfile

the >> will redirect STDOUT to the file "outputfile"

ok, thanks a ton!

np. Glad I could help.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0