FAQ
I am trying to get rid of a blank line at the start of a text file, and
I dont understand why this does not work

open(UPD1,"tem");# this file exists
my @update1=<UPD1>;
foreach $update1(@update1){

$update1=~s/^(\n)//;
$update1=~chomp;
my @update2=split /#/,$update1;# this bit works

But I still get a blank line at the start
ie
print $contact_id,br;# name give to first field
gives

blank line
233
234
235
236
237
238

Search Discussions

  • WC -Sx- Jones at Mar 24, 2004 at 2:01 pm

    mike wrote:
    I am trying to get rid of a blank line at the start of a text file, and
    I dont understand why this does not work

    open(UPD1,"tem");# this file exists
    my @update1=<UPD1>;
    foreach $update1(@update1){

    $update1=~s/^(\n)//;

    while(<UPD1>) {
    chomp; s/^\s+//; s/\s+$//; next unless length;

    $update1 = $_;

    $update1=~chomp;
    my @update2=split /#/,$update1;# this bit works

    But I still get a blank line at the start
    blank line
    233

    What you ask is a FAQ - see Faq 4 at perldoc.com

    --
    _Sx_ http://youve-reached-the.endoftheinternet.org/ _____
    perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
  • James Edward Gray II at Mar 24, 2004 at 2:19 pm

    On Mar 24, 2004, at 7:59 AM, WC -Sx- Jones wrote:

    while(<UPD1>) {
    chomp; s/^\s+//; s/\s+$//; next unless length;
    There's probably not much reason to chomp() and s/\s+$//, since the
    later handles both.

    James
  • WC -Sx- Jones at Mar 24, 2004 at 2:41 pm

    James Edward Gray II wrote:
    On Mar 24, 2004, at 7:59 AM, WC -Sx- Jones wrote:

    while(<UPD1>) {
    chomp; s/^\s+//; s/\s+$//; next unless length;

    There's probably not much reason to chomp() and s/\s+$//, since the
    later handles both.
    Yes, and I forget that the way I used it chomp only
    gets ONE \n --

    $_ = "Hello End of Lines\n\n\n";
    chomp; print "1: $_ this is after.\n\n";

    $_ = "Hello End of Lines\n\n\n";
    s/\s+$//; print "2: $_ this is after.\n\n";


    --
    _Sx_ http://youve-reached-the.endoftheinternet.org/ _____
    perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
  • Mike at Mar 25, 2004 at 10:43 am

    On Wed, 2004-03-24 at 14:19, James Edward Gray II wrote:
    On Mar 24, 2004, at 7:59 AM, WC -Sx- Jones wrote:

    while(<UPD1>) {
    chomp; s/^\s+//; s/\s+$//; next unless length;
    There's probably not much reason to chomp() and s/\s+$//, since the
    later handles both.

    James
    Thanks for all assistance

    This worked
    s/\s+$/;

    One question will this only take out blank lines?
    eg: if I have this line
    anytext tabe space newline
    will the non-printing characters be removed and the text be added to the
    beginning of the next line?

    thanks
  • WC -Sx- Jones at Mar 25, 2004 at 1:29 pm

    mike wrote:

    This worked
    s/\s+$/;

    One question will this only take out blank lines?
    eg: if I have this line
    anytext tabe space newline
    will the non-printing characters be removed and the text be added to the
    beginning of the next line?
    This will only remove whitespace from the END of a line.

    If you slurp the file in a one BIG line ... well, you
    accomplished very little. Removing s,\n,,; forces the whole
    thing to become one text block.

    My while statement in the other post forced line by line processing...

    If you are still having trouble processing the file; maybe
    this trick will slow it down to a reasonable speed:

    http://backpan.cpan.org/authors/id/T/TO/TOMC/scripts/slowcat.gz

    :)

    --
    _Sx_ http://youve-reached-the.endoftheinternet.org/ _____
    perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
  • John W . Krahn at Mar 24, 2004 at 2:07 pm

    On Tuesday 23 March 2004 13:13, mike wrote:
    I am trying to get rid of a blank line at the start of a text file,
    and I dont understand why this does not work

    open(UPD1,"tem");# this file exists
    You should *ALWAYS* verify that the file opened successfully.

    open UPD1, 'tem' or die "Cannot open tem: $!";

    my @update1=<UPD1>;
    foreach $update1(@update1){
    Is there a good reason to read the entire file into an array?

    $update1=~s/^(\n)//;
    That removes a newline if it is the first character in the line. The
    parentheses are not required.

    $update1=~chomp;
    That is the same as:

    $update1 =~ /0/;

    if the previous line removed the newline and:

    $update1 =~ /1/;

    if it did not remove the newline.

    This is probably what you want to do instead:

    foreach my $update1 ( @update1 ) {
    chomp $update1;
    next unless $update1 =~ /\S/;




    John
    --
    use Perl;
    program
    fulfillment
  • John W . Krahn at Mar 24, 2004 at 2:17 pm

    On Wednesday 24 March 2004 06:07, John W. Krahn wrote:
    On Tuesday 23 March 2004 13:13, mike wrote:
    I am trying to get rid of a blank line at the start of a text file,
    and I dont understand why this does not work

    open(UPD1,"tem");# this file exists
    You should *ALWAYS* verify that the file opened successfully.

    open UPD1, 'tem' or die "Cannot open tem: $!";
    my @update1=<UPD1>;
    foreach $update1(@update1){
    Is there a good reason to read the entire file into an array?
    $update1=~s/^(\n)//;
    That removes a newline if it is the first character in the line. The
    parentheses are not required.
    $update1=~chomp;
    That is the same as:

    $update1 =~ /0/;

    if the previous line removed the newline and:

    $update1 =~ /1/;

    if it did not remove the newline.
    My bad ... it will always be:

    $update1 =~ /0/;

    because there is nothing in $_ to chomp.



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 23, '04 at 9:20p
activeMar 25, '04 at 1:29p
posts8
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase