help with excercise - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with excercise

martin23martin23 Posts: 6Member
hi, I am working through the Nik Silver tutorial and have got as far as opening a text file and printing the output with the lines numbered using the following code :-

$file = 'c:
ess.txt'; # Name the file
open(INFO, $file); # Open the file
$g = '001';
while ($line = )
{
print "$g $line";
++$g;
}

I have to manipulate this code so that blank lines are not counted or numbered but are still printed out, i have tried all sorts but can not get it to work, can someone help?
thanks

Comments

  • WeirdofreakWeirdofreak Posts: 439Member
    Put in an if statement to check if it's a blank line, and if so print it, otherwise do what you're already doing.
  • JonathanJonathan Posts: 2,914Member
    : hi, I am working through the Nik Silver tutorial and have got as far
    : as opening a text file and printing the output with the lines
    : numbered using the following code :-
    :
    : $file = 'c:
    ess.txt'; # Name the file
    : open(INFO, $file); # Open the file
    : $g = '001';
    : while ($line = )
    : {
    : print "$g $line";
    : ++$g;
    : }
    :
    : I have to manipulate this code so that blank lines are not counted
    : or numbered but are still printed out, i have tried all sorts but
    : can not get it to work, can someone help?
    A "blank line" is a scalar whose only contents is a newline character in this context, so we can use an if statement to check if $line just contains one of those. So inside of your while loop now looks like this:-

    [code] if ($line ne "
    ") {
    print "$g $line";
    ++$g;
    } else {
    print $line;
    }[/code]

    Hope this helps - good luck with Perl! :-)

    Jonathan


    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.