Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Removing line from a string

by Anonymous Monk
on Jan 09, 2013 at 16:35 UTC ( #1012527=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

All, I'm pretty new to perl and i'm running into a little difficulty. I have a string $records that contains the following:


If somebody could instruct me how to remove the "Key" and the \n from the string I would be most appreciative. It will always be on the first \n

Replies are listed 'Best First'.
Re: Removing line from a string
by choroba (Chancellor) on Jan 09, 2013 at 16:39 UTC
    Remove everything up to the first newline:
    $records =~ s/^ # beginning of the string .* # whatever except newline \n # newline //x;
    Update: Simplified.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Removing line from a string
by si_lence (Deacon) on Jan 09, 2013 at 17:13 UTC
    If it's always "Key" on the first line

     $records = substr($records, 4);

    Or up to the first newline

     $records = substr($records, index($records, "\n")+1);
Allternative method (aka TIMTOWTDI)
by space_monk (Chaplain) on Jan 09, 2013 at 17:34 UTC
    This one puts the lines in an array and removes the first array element - this is probably not the best method, but would be interesting to see how it compares with the regex methods above.
    my @lines = split /^/, $records; shift @lines; # remove first line print @lines; # print the rest
    or use the range operator:
    my @lines = split /^/, $records; print @lines[1..$#lines]; # print line 1 onwards removing line 0
    A Monk aims to give answers to those who have none, and to learn from those who know more.

      It turns out that choroba's regex method performs the best for larger data sets and the index method for small sets.

      #!/usr/bin/perl use Benchmark qw ( :hireswallclock cmpthese timethese ); use strict; use warnings; # medium data set our $records = "Key\n" . join ( "\n", 1..200000 ); # small data set # our $records = 'Key # 1 # 2 # 3 # 4 # 5'; sub choroba { local $records = $records; $records =~ s/^ # beginning of the string .* # whatever except newline \n # newline //x; #print $records, "\nchoroba\n"; } sub si_lence { local $records = $records; $records = substr($records, 4); #print $records, "\nsi_lence\n"; } sub space_monk { local $records = $records; my @lines = split /^/, $records; shift @lines; # remove first line #print @lines; #print "\nspace_monk\n"; } my $results = timethese( -5, { 'choroba' => 'choroba', 'si_lence' => 'si_lence', 'space_monk' => 'space_monk', } ); cmpthese($results); __END__ **** Results with small data set Benchmark: running choroba, si_lence, space_monk for at least 5 CPU se +conds... choroba: 6.08126 wallclock secs ( 6.09 usr + 0.00 sys = 6.09 CPU) + @ 958995.90/s (n=5843162) si_lence: 6.28688 wallclock secs ( 6.28 usr + 0.00 sys = 6.28 CPU) + @ 1221333.07/s (n=7671193) space_monk: 5.64815 wallclock secs ( 5.64 usr + 0.00 sys = 5.64 CPU) + @ 324712.99/s (n=1831706) Rate space_monk choroba si_lence space_monk 324713/s -- -66% -73% choroba 958996/s 195% -- -21% si_lence 1221333/s 276% 27% -- **** Results with medium data set Benchmark: running choroba, si_lence, space_monk for at least 5 CPU se +conds... choroba: 5.05257 wallclock secs ( 2.20 usr + 2.83 sys = 5.03 CPU) + @ 1639.44/s (n=8248) si_lence: 5.87241 wallclock secs ( 3.84 usr + 2.02 sys = 5.86 CPU) + @ 862.29/s (n=5053) space_monk: 5.16132 wallclock secs ( 5.03 usr + 0.09 sys = 5.12 CPU) + @ 3.51/s (n=18) Rate space_monk si_lence choroba space_monk 3.51/s -- -100% -100% si_lence 862/s 24442% -- -47% choroba 1639/s 46560% 90% --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012527]
Approved by Old_Gray_Bear
[Corion]: ambrus: Well, for mathematical notation, I find blackboard much more convenient than a computer. But when inserting text or moving text around, the computer wins obviously
[ambrus]: But either of these is a big problem in practice, so I'd need to spend like thirty years of my life to solve (a) and five more years to solve (b)
[ambrus]: Corion: yes, CURRENTLY the blackboard is more convenient
[ambrus]: and it's not like I want to ban blackboards anyway
[ambrus]: you already have blackboards and a canvas for projector or overhead transparencies (or positive film slide projector, not used for maths) canvas in the same lecture halls today, and switch in a few minutes between presentations,
[ambrus]: they're only difficult to use together.
[ambrus]: overhead transparencies are a nice convenience by the way that mix the two slides, because you can write them in advance and edit them during the presentation easily. but they're not very much in fasion these days.
[ambrus]: you can even print them.
[Discipulus]: ambrus i'm trying out MremoteNG which wrap putty and rdp and many other things..
[Corion]: ambrus: Yes, ideally you would have the ease of overhead projection transparencies and pens drawing on them, combined with the computer generated slide text...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-09-26 10:26 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.