input file

--_000_D225040F2D0F75448937B83D9527991A0154882D3Adenex1crick et_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I'm trying to use file path for my file that I want to read but I am gettin=
g the following error when trying to use strict.

Can't use string ("C://temp//PCMD") as a symbol ref while "strict refs" in =
use at ./DOband.pl line 10.

Any help is greatly appreciated.

#!/usr/bin/perl

use warnings;
use strict;

my $filepath =3D "C://temp//PCMD";
my $outfile =3D "output.txt";


open ("$filepath") || die "ERROR: opening $filepath\n";
open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";



Chris Stinemetz


--_000_D225040F2D0F75448937B83D9527991A0154882D3Adenex1crick et_--
Chris Stinemetz [ Do, 17 März 2011 16:56 ] [ ID #2056731 ]

Re: input file

On Thu, Mar 17, 2011 at 11:56, Chris Stinemetz
<cstinemetz [at] cricketcommunications.com> wrote:
> I'm trying to use file path for my file that I want to read but I am gett=
ing the following error when trying to use strict.
>
> Can't use string ("C://temp//PCMD") as a symbol ref while "strict refs" i=
n use at ./DOband.pl line 10.
>
> Any help is greatly appreciated.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $filepath =3D "C://temp//PCMD";
> my $outfile =C2=A0=3D "output.txt";
>
>
> open ("$filepath") || die "ERROR: opening $filepath\n";
> open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";
>
>
>
> Chris Stinemetz
>
>

The proper syntax is one of

#very old school and bad
our $FILEPATH =3D $filepath;
open FILEPATH;

#dangerous because we don't specify the mode,
#which means if $filepath starts with > we could accidentally
#overwrite another file
open INFILE, $filepath or die "Could not open $filepath: $!";

#better, but still using the old two argument version of open
open INFILE, "< $filepath" or die "Could not open $filepath: $!";

#pretty good, but INFILE is visible to the entire package
#and some other code may already be using it, this is
#not much a problem in small programs, but in larger ones
#it leads to obscure and hard to diagnose bugs
open INFILE, "<", $filepath or die "Could not open $filepath: $!";

#this is the best way, it is safe and $infile is scope to the enclosing blo=
ck
open my $infile, "<", $filepath or die "Could not open $filepath: $!";

You can read more in

http://perldoc.perl.org/functions/open.html

or

perldoc -f open



--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Do, 17 März 2011 17:08 ] [ ID #2056732 ]

Re: input file

On 17/03/2011 15:56, Chris Stinemetz wrote:
>
> I'm trying to use file path for my file that I want to read but I am
> getting the following error when trying to use strict.
>
> Can't use string ("C://temp//PCMD") as a symbol ref while "strict
> refs" in use at ./DOband.pl line 10.
>
> Any help is greatly appreciated.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $filepath = "C://temp//PCMD";

You are getting confused with backward and forward slashes. Backward
slashes are the proper delimiter for Windows file paths, and require
escaping in strings with a second back slash. However Perl is kind and
allows either forward or backward slashes to serve the same purpose, but
a plain forward slash doesn't require escaping.

What you have written contains pairs of forward slashes which, luckily,
is interpreted the same as a single slash, so ultimately your code does
what it is supposed to.

You should either take advantage of Perl's leniency and write

my $filepath = "C:/temp/PCMD";

or use single quotes (within which backslashes need escaping only if
they appear as the final character) and write

my $filepath = 'C:\temp\PCMD';

> my $outfile = "output.txt";
>
>
> open ("$filepath") || die "ERROR: opening $filepath\n";

Except for old-fashioned and exotic syntax, a call to open requires
three parameters:

- The variable to assign the open file handle to
- The open mode (usually read or write)
- The file path and name

Apart from this, it is very wrong to enclose a simple scalar variable in
quotes. Doing so does have a purpose, but it is an esoteric one and is
almost never what you want to do.

It is best to stick to the low-priority 'or' instead of '||' as the
latter will do what you want only if you have put parentheses around the
parameters to the open call.

You should always put the system variable $! into your die string, as it
explains the reason for the failure. It is also a bad idea to terminate
the die string with a newline, as it prevents die from displaying the
source file and line number where the failure occurred.

Putting these together, your code should read

open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";

> open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";

Interesting that you have remembered to include a file handle and an
open mode here! But the other points apply:

open my $outfile, '>', $outfile or die "ERROR opening $outfile: $!";

I hope this helps you.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Do, 17 März 2011 17:36 ] [ ID #2056733 ]

RE: input file

-----Original Message-----
From: Rob Dixon [mailto:rob.dixon [at] gmx.com]
Sent: Thursday, March 17, 2011 10:37 AM
To: beginners
Cc: Chris Stinemetz
Subject: Re: input file

On 17/03/2011 15:56, Chris Stinemetz wrote:
>
> I'm trying to use file path for my file that I want to read but I am
> getting the following error when trying to use strict.
>
> Can't use string ("C://temp//PCMD") as a symbol ref while "strict
> refs" in use at ./DOband.pl line 10.
>
> Any help is greatly appreciated.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $filepath =3D "C://temp//PCMD";

You are getting confused with backward and forward slashes. Backward
slashes are the proper delimiter for Windows file paths, and require
escaping in strings with a second back slash. However Perl is kind and
allows either forward or backward slashes to serve the same purpose, but
a plain forward slash doesn't require escaping.

What you have written contains pairs of forward slashes which, luckily,
is interpreted the same as a single slash, so ultimately your code does
what it is supposed to.

You should either take advantage of Perl's leniency and write

my $filepath =3D "C:/temp/PCMD";

or use single quotes (within which backslashes need escaping only if
they appear as the final character) and write

my $filepath =3D 'C:\temp\PCMD';

> my $outfile =3D "output.txt";
>
>
> open ("$filepath") || die "ERROR: opening $filepath\n";

Except for old-fashioned and exotic syntax, a call to open requires
three parameters:

- The variable to assign the open file handle to
- The open mode (usually read or write)
- The file path and name

Apart from this, it is very wrong to enclose a simple scalar variable in
quotes. Doing so does have a purpose, but it is an esoteric one and is
almost never what you want to do.

It is best to stick to the low-priority 'or' instead of '||' as the
latter will do what you want only if you have put parentheses around the
parameters to the open call.

You should always put the system variable $! into your die string, as it
explains the reason for the failure. It is also a bad idea to terminate
the die string with a newline, as it prevents die from displaying the
source file and line number where the failure occurred.

Putting these together, your code should read

open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";

> open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";

Interesting that you have remembered to include a file handle and an
open mode here! But the other points apply:

open my $outfile, '>', $outfile or die "ERROR opening $outfile: $!";

I hope this helps you.

Rob

Not sure what I am doing wrong but when I incorporate the infile that I wan=
t to read and process the program does nothing and it seems like my PCMD fi=
le is never opened.

Below is my code thus far.

Thank you,

Chris

#!/usr/bin/perl

use warnings;
use strict;

#my $filepath =3D 'PCMD';
my $filepath =3D 'C:/temp/PCMD';
my $outfile =3D 'output.txt';

open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
open my $out, '>', $outfile or die "ERROR opening $outfile: $!";

my [at] array;

while (<>) {
next unless /;/;
chomp;
my [at] data =3D ( split /;/ )[31,32,38,261];
push [at] array, join "\t", [at] data;
}

[at] array =3D sort {
my [at] aa =3D split /\t/, $a;
my [at] bb =3D split /\t/, $b;
$aa[0] <=3D> $bb[0] or
$aa[1] <=3D> $bb[1] or
$aa[2] <=3D> $bb[2];
} [at] array;

print $out "$_\n" foreach [at] array; =09
close $out;



#RTD 1 unit =3D 4 chips RUM Field 16 0 - 2^16 - 1 milliseconds








--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Chris Stinemetz [ Do, 17 März 2011 20:26 ] [ ID #2056742 ]

Re: input file

On 3/17/11 Thu Mar 17, 2011 12:26 PM, "Chris Stinemetz"
<cstinemetz [at] cricketcommunications.com> scribbled:


> Rob
>
> Not sure what I am doing wrong but when I incorporate the infile that I want
> to read and process the program does nothing and it seems like my PCMD file is
> never opened.
>
> Below is my code thus far.
>
> Thank you,
>
> Chris
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> #my $filepath = 'PCMD';
> my $filepath = 'C:/temp/PCMD';
> my $outfile = 'output.txt';
>
> open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
> open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
>
> my [at] array;
>
> while (<>) {

The above line needs to be:

while (<$fh>) {


> next unless /;/;
> chomp;
> my [at] data = ( split /;/ )[31,32,38,261];
> push [at] array, join "\t", [at] data;
> }
>
> [at] array = sort {
> my [at] aa = split /\t/, $a;
> my [at] bb = split /\t/, $b;
> $aa[0] <=> $bb[0] or
> $aa[1] <=> $bb[1] or
> $aa[2] <=> $bb[2];
> } [at] array;
>
> print $out "$_\n" foreach [at] array;
> close $out;

Can you get your email client to add "quoting" symbols to the beginning of
the lines that were in the message to which you are responding (like the '>
' in this message)? That would make it easier for people to tell what you
have written and what was written before.

Thanks.



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Do, 17 März 2011 20:40 ] [ ID #2056743 ]
Perl » gmane.comp.lang.perl.beginners » input file

Vorheriges Thema: sort results in ascending order
Nächstes Thema: ip address substitution