Delete characters

I

imphasing

I need to delete the first five characters of a string. Is there an
easy way to do this? It doesn't matter what character, I just need to
delete the first 5. I have a string like this:

45ghsHello There!

or maybe

hf745Hello There!

and I want to get rid of the 45ghs or hf745.

thanks,
Alex
 
J

jl_post

I need to delete the first five characters of a string. Is there an
easy way to do this? It doesn't matter what character, I just need to
delete the first 5. I have a string like this:

45ghsHello There!

or maybe

hf745Hello There!

and I want to get rid of the 45ghs or hf745.


Dear Alex,

Fortunately for you, there is an easy way to do this.

Say you have a string like:

my $string = "45ghsHello There!";

You can remove the first five characters like this:

substr($string, 0, 5, "");

or even like this:

substr($string, 0, 5) = "";


To read more about substr() and what it can do, type:

perldoc -f substr

at any DOS or UNIX prompt.

I hope this helps, Alex.

-- Jean-Luc
 
J

John W. Krahn

I need to delete the first five characters of a string. Is there an
easy way to do this? It doesn't matter what character, I just need to
delete the first 5. I have a string like this:

45ghsHello There!

or maybe

hf745Hello There!

and I want to get rid of the 45ghs or hf745.

my $string = '45ghsHello There!';

$string = substr $string, 5;

Or:

$string = unpack 'x5 a*', $string

Or:

$string =~ s/^.{5}//s;



John
 
I

imphasing

now, does anyone know how I would delete all but the LAST 500 lines of
a text file?
I was thinking of a while loop, but my brain fizzled out, and I
couldn't figure out how to do it...any suggestions?

Thanks,
Alex
 
E

Eric Schwartz

imphasing said:
now, does anyone know how I would delete all but the LAST 500 lines of
a text file?

Use a circular buffer. Or File::ReadBackwards, I guess.

-=Eric
 
J

jl_post

imphasing said:
now, does anyone know how I would delete all but
the LAST 500 lines of a text file?


Here's a "quick and dirty" way to do it:

# Read the file contents into $text.
$text = $1 if $text =~ m/((.*\n){500})$/;
# Print $text back out to a file.

(This may not be the most efficient way to do what you want, but it
works.)

-- Jean-Luc
 
A

Axel

imphasing said:
now, does anyone know how I would delete all but the LAST 500 lines of
a text file?
I was thinking of a while loop, but my brain fizzled out, and I
couldn't figure out how to do it...any suggestions?

Maybe something like:

my $file = "qb";
my @line = split /\s+/, `wc -l $file`;
open INFILE, $file or die "Can't open file";
<INFILE> for (1 .. $line[1] - 500);
while (<INFILE>) {
print; # Or do something else
}

Axel
 
A

Anno Siegel

Here's a "quick and dirty" way to do it:

# Read the file contents into $text.
$text = $1 if $text =~ m/((.*\n){500})$/;
# Print $text back out to a file.

(This may not be the most efficient way to do what you want, but it
works.)

It is more effort to read a file in a scalar variable than to read
the lines into an array. Since the problem is line-oriented, that
would be a better solution:

my @text = <FILE>;
print @text[ -500 .. -1];

Anno
 
J

John Bokma

Axel said:
Maybe something like:

my $file = "qb";
my @line = split /\s+/, `wc -l $file`;

Uhm, you have heard of tail?

man tail

[...]

-n, --lines=N
output the last N lines, instead of the last 10
[...]
 
A

Anno Siegel

Here's a "quick and dirty" way to do it:

# Read the file contents into $text.
$text = $1 if $text =~ m/((.*\n){500})$/;
# Print $text back out to a file.

(This may not be the most efficient way to do what you want, but it
works.)

It is more effort to read a file in a scalar variable than to read
the lines into an array. Since the problem is line-oriented, that
would be a better solution:

my @text = <FILE>;
print @text[ -500 .. -1];

Anno
 
J

John W. Krahn

Axel said:
Maybe something like:

my $file = "qb";
my @line = split /\s+/, `wc -l $file`;

Why split() and the array?

chomp( my $line = `wc -l < $file` );

open INFILE, $file or die "Can't open file";
<INFILE> for (1 .. $line[1] - 500);
while (<INFILE>) {
print; # Or do something else
}


John
 
J

jl_post

Anno said:
It is more effort to read a file in a scalar
variable than to read the lines into an array.
Since the problem is line-oriented, that
would be a better solution:

my @text = <FILE>;
print @text[ -500 .. -1];


I thought about that solution, but I decided not to mention it
because it would generate warnings if the number of lines were less
than 500 (of course, "use warnings;" would have to be turned on as
well).

Giving this more though, I find that this problem is easily
prevented with the following line:

@text = @text[-500 .. -1] if @text > 500;

This line uses the same "if" logic as my solution, while maintaining
the better efficiency of yours.

-- Jean-Luc
 
A

Anno Siegel

Anno said:
It is more effort to read a file in a scalar
variable than to read the lines into an array.
Since the problem is line-oriented, that
would be a better solution:

my @text = <FILE>;
print @text[ -500 .. -1];


I thought about that solution, but I decided not to mention it
because it would generate warnings if the number of lines were less
than 500 (of course, "use warnings;" would have to be turned on as
well).

Giving this more though, I find that this problem is easily
prevented with the following line:

@text = @text[-500 .. -1] if @text > 500;

This line uses the same "if" logic as my solution, while maintaining
the better efficiency of yours.

That's correct, though I was aiming for clarity more than efficiency.
Your solution keeps @text intact if there are fewer than 500 elements.
That is the behavior one usually wants in that case.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top