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.

New to Perl and stuck...Help

mikeruthmikeruth Posts: 4Member
[b][red]This message was edited by mikeruth at 2003-12-18 13:12:55[/red][/b][hr]
[b][red]This message was edited by mikeruth at 2003-12-18 13:12:10[/red][/b][hr]
I am on my second day of Perl programming and I am stuck. The program
below will read a one line, 1 to 2 M file, test it, and, print some counts to another file. I cannot get past the open...infile.

Help! I need to get this done soon (and with "use strict").

Where did all of my spaces go???

========================= The Code ===========================

#!/opt/utils/5bin/perl5.001 -w

# We have two versions. Make sure to use the lastest.

require 5.001;
use strict;

if ( @ARGV < 2 )
{
die "
Usage: $0

Stopped";
}

my $infile = shift @ARGV;
my $outfile = shift @ARGV;

open( my $infile_handle, "< $infile" ) or
die "
Can't open input file "$infile".
",
"Error: $!.
",
"Stopped";

open( my $outfile_handle, "> $outfile" ) or
die "
Can't open output file "$outfile".
",
"Error: $!.
",
"Stopped";

my $text = join( '', <$infile_handle> );
my $test_len = length( $test );
print "
" , $test_len, "
";

close $infile_handle;

chomp $text;

#
# process the line and print results...
#

close $outfile_handle;

exit ( 0 );





Comments

  • JonathanJonathan Posts: 2,914Member
    : I am on my second day of Perl programming and I am stuck. The ]
    : program below will read a one line, 1 to 2 M file, test it, and,
    : print some counts to another file. I cannot get past the
    : open...infile.
    :
    : Help! I need to get this done soon (and with "use strict").
    :
    : Where did all of my spaces go???
    :
    : ========================= The Code ===========================
    :
    : #!/opt/utils/5bin/perl5.001 -w
    :
    : # We have two versions. Make sure to use the lastest.
    :
    : require 5.001;
    : use strict;
    :
    : if ( @ARGV < 2 )
    : {
    : die "
    Usage: $0

    Stopped";
    : }
    :
    : my $infile = shift @ARGV;
    : my $outfile = shift @ARGV;
    :
    : open( my $infile_handle, "< $infile" ) or
    : die "
    Can't open input file "$infile".
    ",
    : "Error: $!.
    ",
    : "Stopped";
    Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-

    my $infile_handle;
    open $infile_handle, "< $infile" or ...blah...;

    : open( my $outfile_handle, "> $outfile" ) or
    : die "
    Can't open output file "$outfile".
    ",
    : "Error: $!.
    ",
    : "Stopped";
    And maybe the same there.

    : my $text = join( '', <$infile_handle> );
    : my $test_len = length( $test );
    Should that not be:-
    my $test_len = length( $te[red]x[/red]t );

    : print "
    " , $test_len, "
    ";
    :
    : close $infile_handle;
    :
    : chomp $text;
    :
    : #
    : # process the line and print results...
    : #
    :
    : close $outfile_handle;
    :
    : exit ( 0 );

    Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained. :-)

    Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.

    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.");

  • mikeruthmikeruth Posts: 4Member
    [b][red]This message was edited by mikeruth at 2003-12-18 14:28:27[/red][/b][hr]
    [b][red]This message was edited by mikeruth at 2003-12-18 14:25:13[/red][/b][hr]
    I made the changes that you suggested (thanks for catching the "test"
    error). The new code is:

    my $infile_handle;
    open( $infile_handle, "< $infile" ) or ...

    When run I get:

    Can't use an undefined value as a symbol reference at edijd110.pl line 24. <--- the open above

    With -c I get:

    edijd110.pl syntax OK


    : : open( my $infile_handle, "< $infile" ) or
    : : die "
    Can't open input file "$infile".
    ",
    : : "Error: $!.
    ",
    : : "Stopped";
    : Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-
    :
    : my $infile_handle;
    : open $infile_handle, "< $infile" or ...blah...;
    :
    : : open( my $outfile_handle, "> $outfile" ) or
    : : die "
    Can't open output file "$outfile".
    ",
    : : "Error: $!.
    ",
    : : "Stopped";
    : And maybe the same there.
    :
    : Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained. :-)
    :
    : Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.
    :
    : 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.");
    :
    :





  • mikeruthmikeruth Posts: 4Member
    I found a solution but I do not know why it works.

    I deleted the line "my $infile_handle;".
    I changed all occurences of "$infile_handle" to "INFILE_HANDLE".

    Why does this work?



    : [b][red]This message was edited by mikeruth at 2003-12-18 14:28:27[/red][/b][hr]
    : [b][red]This message was edited by mikeruth at 2003-12-18 14:25:13[/red][/b][hr]
    : I made the changes that you suggested (thanks for catching the "test"
    : error). The new code is:
    :
    : my $infile_handle;
    : open( $infile_handle, "< $infile" ) or ...
    :
    : When run I get:
    :
    : Can't use an undefined value as a symbol reference at edijd110.pl line 24. <--- the open above
    :
    : With -c I get:
    :
    : edijd110.pl syntax OK
    :
    :
    : : : open( my $infile_handle, "< $infile" ) or
    : : : die "
    Can't open input file "$infile".
    ",
    : : : "Error: $!.
    ",
    : : : "Stopped";
    : : Don't see an immediate problem here, but you are using an older version of Perl. This means it may like you to declare $infile_handle before the open statement, e.g.:-
    : :
    : : my $infile_handle;
    : : open $infile_handle, "< $infile" or ...blah...;
    : :
    : : : open( my $outfile_handle, "> $outfile" ) or
    : : : die "
    Can't open output file "$outfile".
    ",
    : : : "Error: $!.
    ",
    : : : "Stopped";
    : : And maybe the same there.
    : :
    : : Looks OK besides that. FYI, it's good to enclose code in code and /code tags - tags are put in square brackets. Then the spacing is retained. :-)
    : :
    : : Hope this helps, if not please post the output of perl -c thescript.pl, where thescript.pl is your script. That or run it and post the warnings that you're given.
    : :
    : : 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.");
    : :
    : :
    :
    :
    :
    :
    :
    :

  • JonathanJonathan Posts: 2,914Member
    : I found a solution but I do not know why it works.
    :
    : I deleted the line "my $infile_handle;".
    : I changed all occurences of "$infile_handle" to "INFILE_HANDLE".
    That was going to be my suggestion, but I went to grab some whisky before replying and you beat me to it. :-)

    : Why does this work?
    In older versions of Perl 5 it was not possible to store a file handle in a scalar. 5.005 qualifies as "older". :-)

    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.");

  • mikeruthmikeruth Posts: 4Member
    Thank you for your help and time.


    : : I found a solution but I do not know why it works.
    : :
    : : I deleted the line "my $infile_handle;".
    : : I changed all occurences of "$infile_handle" to "INFILE_HANDLE".
    : That was going to be my suggestion, but I went to grab some whisky before replying and you beat me to it. :-)
    :
    : : Why does this work?
    : In older versions of Perl 5 it was not possible to store a file handle in a scalar. 5.005 qualifies as "older". :-)
    :
    : 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.