
sub routine
--_000_D225040F2D0F75448937B83D9527991A0154882FE8denex1crick et_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Thanks Chris
I am trying to build a sub routine that computes the following equation
When $data[262] has the value of 1
Take the value of $data[261] and return the solution of: $data[261]+13)/6.6=
/8/2*10)/10
When I call the sub routine before I push the array I would like to return =
the value in 0.1 decimal.
#!/usr/bin/perl
use warnings;
use strict;
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 =3D ();
while (<$fh>){
next unless /;/;
chomp;
my [at] data =3D split /;/;
my($cell,$sect,$chan,$carr,$dist,$precis) =3D ($data[31],$data[32],$dat=
a[38],$data[39],$data[261],$data[262]);
if ($data[38] =3D=3D 15)
{
$data[39] =3D 2;
} else {
$data[39] =3D 1;
}
if( length($data[261]) > 12=
)
{
$dist =3D g=
etDist;
}
else
{
$dist =3D "=
";
}
push [at] array, {
cell =3D> $cell,
sect =3D> $sect,
carr =3D> $carr,
RTD =3D> $RTD,
};
}
my [at] sorted =3D map { $_->[0] }
sort {
$a->[1] <=3D> $b->[1]
|| $a->[2] <=3D> $b->[2]
|| $a->[3] <=3D> $b->[3]
}
map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
[at] array;
for my $tuple ( [at] sorted ){
print "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}\n";
}
for my $tuple ( [at] sorted ){
print $out "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}\=
n";
}
close $out;
############################################################ ###
# Round Trip Delay
# Indicates the precision of the Round Trip Delay. If 1, Round
# Trip Delay is in units of 1/8 chip (0.125). If 32, Round Trip Delay
# is in units of 4 chip.
--_000_D225040F2D0F75448937B83D9527991A0154882FE8denex1crick et_--
Re: sub routine
>>>>> "CS" == Chris Stinemetz <cstinemetz [at] cricketcommunications.com> writes:
CS> Thanks Chris
CS> I am trying to build a sub routine that computes the following equation
CS> When $data[262] has the value of 1
CS> Take the value of $data[261] and return the solution of: $data[261]+13)/6.6/8/2*10)/10
CS> When I call the sub routine before I push the array I would like
CS> to return the value in 0.1 decimal.
that makes no sense. a floating number is just what it is. only when
printing it does counting decimal places make sense.
CS> use warnings;
CS> use strict;
CS> my $filepath = 'C:/temp/PCMD';
CS> my $outfile = 'output.txt';
CS> open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
CS> open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
CS> my [at] array = ();
CS> while (<$fh>){
better to assign the line to a variable. using $_ all the time isn't a
great idea for several reasons. the topmost one is that named variables
make the program more readable.
CS> next unless /;/;
CS> chomp;
CS> my [at] data = split /;/;
CS> my($cell,$sect,$chan,$carr,$dist,$precis) = ($data[31],$data[32],$data[38],$data[39],$data[261],$data[26 2]);
use a slice to save typing and eyestrain. you can access multiple
array/hash elements at one time:
my($cell,$sect,$chan,$carr,$dist,$precis) = [at] data[31,32,38,39,261,262];
CS> if ($data[38] == 15)
why are you using [at] data again when you just assigned 38 to $chan? use
the scalar so it makes more sense to the reader.
rule: always write code so the reader will understand it.
CS> {
CS> $data[39] = 2;
CS> } else {
CS> $data[39] = 1;
CS> }
again, use the scalar. and i don't see $data[39] used anywhere else so
that assignment is lost. also use the conditional expression. that whole
thing reduces to this:
$carr = ( $chan == 15 ) ? 2 : 1 ;
also since you are overwriting $chan there, you don't need it in the
slice above. $carr can be declared right here too.
CS> if( length($data[261]) > 12)
CS> {
CS> $dist = getDist;
CS> }
CS> else
CS> {
CS> $dist = "";
CS> }
ditto here.
my $dist = ( length( $dist ) > 1 ) ? getDist() : '' ;
CS> push [at] array, {
don't name arrays, [at] array. pick a name that describes the data in it.
CS> cell => $cell,
CS> sect => $sect,
CS> carr => $carr,
CS> RTD => $RTD,
where does $RTD come from? i don't see it anywhere else so it will fail
under strict.
CS> };
CS> }
CS> my [at] sorted = map { $_->[0] }
CS> sort {
CS> $a->[1] <=> $b->[1]
CS> || $a->[2] <=> $b->[2]
CS> || $a->[3] <=> $b->[3]
CS> }
CS> map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
CS> [at] array;
gack. i won't even address that now. but look at Sort::Maker on cpan for
an easier and more descriptive way to sort complex things.
also since you already have the values in a hash, there is no reason to
assign them to an array in there.
CS> for my $tuple ( [at] sorted ){
CS> print "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}\n";
CS> }
slice to the rescue again:
print join( ' ', [at] tuple{ qw( cell sect carr RTD ), "\n" ;
CS> for my $tuple ( [at] sorted ){
CS> print $out "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}\n";
CS> }
CS> close $out;
but why print the same thing twice? store the string in a var, and then
print to both places. or even better, use File::Slurp and simplify.
here is a complete rewrite (untested):
use warnings;
use strict;
use File::Slurp ;
my $filepath = 'C:/temp/PCMD';
my $outfile = 'output.txt';
my %cols = (
cell => 31,
sect => 32,
chan => 38,
dist => 261,
precis => 262,
) ;
my [at] records ;
my [at] lines = read_file( $filepath ) ;
chomp [at] lines ;
foreach my $line ( [at] lines ) {
next unless $line =~ /;/ ;
my %record ;
# this gets just what you want into a hash using a hash slice and an
# array slice. the order of keys and values will be the same for any
# given hash
[at] record{ keys %cols } = (split /;/, $line)[ values %cols ] ;
$record{carr} = ( $record{chan} == 15 ) ? 2 : 1 ;
$record{dist} = ( length( $record{dist}) > 1 ) ? getDist() : '' ;
push( [at] records, $record ) ;
}
my [at] sorted = sort {
$a->{cell} <=> $b->{cell} ||
$a->{sect} <=> $b->{sect} ||
$a->{carr} <=> $b->{carr}
} [at] records ;
my [at] report = map " [at] {$_{ keys %cols }}\n", [at] records ;
print [at] report ;
write_file( $output, [at] report ) ;
now isn't that a lot cleaner? some parts (all the slicing) may take a
little understanding but that isn't too hard.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: sub routine
> >>>>> "CS" =3D=3D Chris Stinemetz <cstinemetz [at] cricketcommunications.com> =
writes:
> CS> Thanks Chris
> CS> I am trying to build a sub routine that computes the following equati=
on
> CS> When $data[262] has the value of 1
> CS> Take the value of $data[261] and return the solution of: $data[261]+1=
3)/6.6/8/2*10)/10
> CS> When I call the sub routine before I push the array I would like
> CS> to return the value in 0.1 decimal.
> that makes no sense. a floating number is just what it is. only when
> printing it does counting decimal places make sense.
> CS> use warnings;
> CS> use strict;
> CS> my $filepath =3D 'C:/temp/PCMD';
> CS> my $outfile =3D 'output.txt';
> CS> open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
> CS> open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
> CS> my [at] array =3D ();
> CS> while (<$fh>){
> better to assign the line to a variable. using $_ all the time isn't a
> great idea for several reasons. the topmost one is that named variables
> make the program more readable.
> CS> next unless /;/;
> CS> chomp;
> CS> my [at] data =3D split /;/;
> CS> my($cell,$sect,$chan,$carr,$dist,$precis) =3D ($data[31],$data[32=
],$data[38],$data[39],$data[261],$data[262]);
> use a slice to save typing and eyestrain. you can access multiple
> array/hash elements at one time:
> my($cell,$sect,$chan,$carr,$dist,$precis) =3D [at] data[31,32,38,39,261,262];
> CS> if ($data[38] =3D=3D 15)
> why are you using [at] data again when you just assigned 38 to $chan? use
> the scalar so it makes more sense to the reader.
> rule: always write code so the reader will understand it.
> CS> {
> CS> $data[39] =3D 2;
> CS> } else {
> CS> $data[39] =3D 1;
> CS> }
> again, use the scalar. and i don't see $data[39] used anywhere else so
> that assignment is lost. also use the conditional expression. that whole
> thing reduces to this:
> $carr =3D ( $chan =3D=3D 15 ) ? 2 : 1 ;
> also since you are overwriting $chan there, you don't need it in the
> slice above. $carr can be declared right here too.
> CS> if( length($data[261]=
) > 12)
> CS> {
> CS> $dist=
=3D getDist;
> CS> }
> CS> else
> CS> {
> CS> $dist=
=3D "";
> CS> }
> ditto here.
> my $dist =3D ( length( $dist ) > 1 ) ? getDist() : '' ;
> CS> push [at] array, {
> don't name arrays, [at] array. pick a name that describes the data in it.
> CS> cell =3D> $cell,
> CS> sect =3D> $sect,
> CS> carr =3D> $carr,
> CS> RTD =3D> $RTD,
> where does $RTD come from? i don't see it anywhere else so it will fail
> under strict.
> CS> };
> CS> }
> CS> my [at] sorted =3D map { $_->[0] }
> CS> sort {
> CS> $a->[1] <=3D> $b->[1]
> CS> || $a->[2] <=3D> $b->[2]
> CS> || $a->[3] <=3D> $b->[3]
> CS> }
> CS> map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
> CS> [at] array;
> gack. i won't even address that now. but look at Sort::Maker on cpan for
> an easier and more descriptive way to sort complex things.
> also since you already have the values in a hash, there is no reason to
> assign them to an array in there.
> CS> for my $tuple ( [at] sorted ){
> CS> print "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->{RTD}=
\n";
> CS> }
> slice to the rescue again:
> print join( ' ', [at] tuple{ qw( cell sect carr RTD ), "\n" ;
> CS> for my $tuple ( [at] sorted ){
> CS> print $out "$tuple->{cell} $tuple->{sect} $tuple->{carr} $tuple->=
{RTD}\n";
> CS> }
> CS> close $out;
> but why print the same thing twice? store the string in a var, and then
> print to both places. or even better, use File::Slurp and simplify.
> here is a complete rewrite (untested):
> use warnings;
> use strict;
> use File::Slurp ;
> my $filepath =3D 'C:/temp/PCMD';
> my $outfile =3D 'output.txt';
> my %cols =3D (
> cell =3D> 31,
> sect =3D> 32,
> chan =3D> 38,
> dist =3D> 261,
> precis =3D> 262,
> ) ;
> my [at] records ;
> my [at] lines =3D read_file( $filepath ) ;
> chomp [at] lines ;
> foreach my $line ( [at] lines ) {
> next unless $line =3D~ /;/ ;
> my %record ;
> # this gets just what you want into a hash using a hash slice and an
> # array slice. the order of keys and values will be the same for any
> # given hash
> [at] record{ keys %cols } =3D (split /;/, $line)[ values %cols ] ;
> $record{carr} =3D ( $record{chan} =3D=3D 15 ) ? 2 : 1 ;
> $record{dist} =3D ( length( $record{dist}) > 1 ) ? getDist() : '' ;
> push( [at] records, $record ) ;
> }
> my [at] sorted =3D sort {
> $a->{cell} <=3D> $b->{cell} ||
> $a->{sect} <=3D> $b->{sect} ||
> $a->{carr} <=3D> $b->{carr}
> } [at] records ;
> my [at] report =3D map " [at] {$_{ keys %cols }}\n", [at] records ;
> print [at] report ;
> write_file( $output, [at] report ) ;
> now isn't that a lot cleaner? some parts (all the slicing) may take a
> little understanding but that isn't too hard.
> uri
> --
> Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.co=
m --
> ----- Perl Code Review , Architecture, Development, Training, Support --=
----
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -----=
----
Thanks for the explanations. This really cleaned up my code. Do you think y=
ou could explain to me how to get
my dist sub routine to work correctly? I am very new to perl so please forg=
ive my ignorance.
I am getting the following errors.
syntax error at ./DOband1.pl line 12, near "$record{dist"
syntax error at ./DOband1.pl line 12, near "13)"
Execution of ./DOband1.pl aborted due to compilation errors.
My updated code is on the bottom.
#!/usr/bin/perl
use warnings;
use strict;
use File::Slurp;
my $filepath =3D 'C:/temp/PCMD';
my $output =3D 'output.txt';
sub Dist {
my $record{dist}+13)/6.6/8/2*10)/10 # I'm am not sure how to handle t=
his.
}
my %cols =3D (
cell =3D> 31,
sect =3D> 32,
chan =3D> 38,
dist =3D> 261,
precis =3D> 262,
);
my [at] records;
my [at] lines =3D read_file( $filepath );
chomp [at] lines;
foreach my $line ( [at] lines ) {
next unless $line =3D~ /;/;
my %record;
# this gets just what you want into a hash using a hash slice and an # arra=
y slice.
# the order of keys and values will be the same for any # given hash
[at] record{ keys %cols } =3D (split /;/, $line)[ values %cols ] ;
$record{carr} =3D ( $record{chan} =3D=3D 15 ) ? 2 : 1 ;
$record{dist} =3D ( length( $record{dist}) > 1 ) ? getDist() : '' ;
push( [at] records, my $record ) ;
}
my [at] sorted =3D sort {
$a->{cell} <=3D> $b->{cell} ||
$a->{sect} <=3D> $b->{sect} ||
$a->{carr} <=3D> $b->{carr}
} [at] records ;
my [at] report =3D map " [at] {$_{ keys %cols }}\n", [at] records ;
print [at] report ;
write_file($output, [at] report) ;
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sub routine
>>>>> "CS" =3D=3D Chris Stinemetz <cstinemetz [at] cricketcommunications.com> wr=
ites:
first off please don't quote ENTIRE emails. edit the quoted part to what
you are commenting about. i can't easily find your stuff in all of my
older emails. communication! that is true in technical emails as it is
in coding. make your stuff readable.
CS> sub Dist {
CS> my $record{dist}+13)/6.6/8/2*10)/10 # I=E2=80=99m am not sure h=
ow to handle
CS> this.
CS> }
you don't know how subs get their arguments. first read perldoc perlsub
and learn that. then come back here. you shouldn't be accessing an
external var in that sub, it should be passed in as an argument.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: sub routine
VGhhbmtzIFVyaS4NCg0KSSd2ZSBiZWVuIHJlYWRpbmcgcGVybGRvYyBwZXJs c3ViIGFuZCBoYXZl
IGEgYmV0dGVyIHVuZGVyc3RhbmRpbmcgYWJvdXQgc3Vicm91dGluZXMsIGJ1 dCBJIGFtIHN0aWxs
IHN0dWNrLiBBbnkgaGVscCBpcyBncmVhdGx5IGFwcHJlY2lhdGVkLg0KDQpU aGUgZXJyb3IgSSBh
bSBnZXR0aW5nIGlzOg0KDQpHbG9iYWwgc3ltYm9sICIlcmVjb3JkIiByZXF1 aXJlcyBleHBsaWNp
dCBw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sNCgkJJGEtPntjZWxsfSA8PT4gJGItPntjZWxs fSB8fA0KCQkkYS0+
e3NlY3R9IDw9PiAkYi0+e3NlY3R9IHx8DQoJCSRhLT57Y2Fycn0gPD0+ICRi LT57Y2Fycn0NCgl9
IEByZWNvcmRzIDsNCg0KDQpteSBAcmVwb3J0ID0gbWFwICJAeyRfeyBrZXlz ICVjb2xzIH19XG4i
LCBAcmVjb3JkcyA7DQoNCnByaW50IEByZXBvcnQgOw0Kd3JpdGVfZmlsZSgk b3V0cHV0LCBAcmVw
b3J0KSA7DQo=
RE: sub routine
Hi Christ
In sub Dist() , you need define scope of each variable as my or =
our or local , which is line no.20 .
Regards
Sunita
-----Original Message-----
From: Chris Stinemetz [mailto:cstinemetz [at] cricketcommunications.com]
Sent: Friday, March 18, 2011 9:18 PM
To: Uri Guttman
Cc: beginners
Subject: RE: sub routine
Thanks Uri.
I've been reading perldoc perlsub and have a better understanding about =
subroutines, but I am still stuck. Any help is greatly appreciated.
The error I am getting is:
Global symbol "%record" requires explicit package name at ./DOband1.pl =
line 20.
Global symbol "$line" requires explicit package name at ./DOband1.pl =
line 20.
Global symbol "%record" requires explicit package name at ./DOband1.pl =
line 21.
syntax error at ./DOband1.pl line 21, near "$record{dist"
Global symbol "%record" requires explicit package name at ./DOband1.pl =
line 22.
syntax error at ./DOband1.pl line 22, near "data["
Global symbol "%record" requires explicit package name at ./DOband1.pl =
line 24.
syntax error at ./DOband1.pl line 25, near "}"
Execution of ./DOband1.pl aborted due to compilation errors.
#!/usr/bin/perl
use warnings;
use strict;
use File::Slurp;
my $filepath =3D 'C:/temp/PCMD';
my $output =3D 'output.txt';
my %cols =3D (
cell =3D> 31,
sect =3D> 32,
chan =3D> 38,
dist =3D> 261,
pr=E9cis =3D> 262,
);
sub Dist{
[at] record{ keys %cols } =3D (split /;/, $line)[ values %cols ] ;
foreach $record{dist} {
$record{dist} =3D ((data[261]+13)/6.6/8/2*10)/10 ;
}
return $record{dist};
}
my [at] records;
my [at] lines =3D read_file( $filepath );
chomp [at] lines;
foreach my $line ( [at] lines ) {
next unless $line =3D~ /;/;
my %record;
# this gets just what you want into a hash using a hash slice and an # =
array slice.
# the order of keys and values will be the same for any # given hash
[at] record{ keys %cols } =3D (split /;/, $line)[ values %cols ] ;
$record{carr} =3D ( $record{chan} =3D=3D 15 ) ? 2 : 1 ;=09
$record{dist} =3D ( length( $record{dist}) > 1 ) ? getDist() : '' ;
push( [at] records, my $record ) ;
}
my [at] sorted =3D sort {
$a->{cell} <=3D> $b->{cell} ||
$a->{sect} <=3D> $b->{sect} ||
$a->{carr} <=3D> $b->{carr}
} [at] records ;
my [at] report =3D map " [at] {$_{ keys %cols }}\n", [at] records ;
print [at] report ;
write_file($output, [at] report) ;
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: sub routine
On Thu, 17 Mar 2011 19:25:02 -0600, Chris Stinemetz wrote:
> Take the value of $data[261] and return the solution of:
> $data[261]+13)/6.6/8/2*10)/10
Your expression there is missing left parentheses. If we guess at where
to put them we end up with
(($data[261]+13)/6.6/8/2*10)/10
which doesn't make sense because why multiply by 10 only to divide by 10?
The code you posted is an odd mix of sophistication levels. Did you
write it all, and if not, do you understand it? You seem to be in need
more of a beginner's lesson to Perl if you need to know how to construct
expressions like that and yet are working with code containing
Schwartzian transforms.
In general, posting a lengthy program and asking how to enhance it to
meet some new requirement is not what this list/group is about; that
falls under "doing your homework". We're here to teach you how to solve
problems, not solve them for you. You might still get solutions - like
you did here - but things will go better when you ask for learning
instead.
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/