Antonio Jose wrote:
>
> Hello.... I have 3 weeks learning Perl and I am trying to solve a trouble
> I need to write my thesis....
>
> I have to read a file where I don't know the content of the first rows
> (only strings) and I need to read only data (numbers), I mean, I am going
> to read information that begin with blank spaces and numbers, from there,
> I have a matrix of 7 columns and n rows (I don't know the exact numbers
> of rows), the column 1 correspond to depth and the next 6 columns
> correspond to variables in each depth.
>
> But there are cases where I have missing values, represented by -999.25,
> in this cases I have to create a couple of columns; one of them
> correponding to the variable without the missing value and teh other one
> with the corresponding depth.
>
> At the end, I will work with matrices of 2 columns (depth and variable),
> called, for example, @rt (variable) and @prof_rt (depth). I write a
> subroutine called "guardar" whose parameters are the vector of depth
> without missing values, the vector of the variable without missing
> values, the vector of the initial variable (with missing values), a
> scalar ($p) that indicate the number of the column (variable) I am
> working on and a scalar ($long) that indicate the length of the vector
> depth without missing values (I could obviate the last two parameters but
> I don't know how to do it). The objective of this subroutine is print in
> other file columns already mencioned (prof_obj, variable, variable
> without missing values) and other column (@intens) obtained by a
> processing done in other sub explained down. This process is executed for
> each value of prof_obj and this is included in a loop for.
>
> On the other hand, the subroutine "suavizar" do the calculus of @intens;
> it takes each value of prof_obj inserted in the sub "guardar" and compare
> it with prof_val (depth related to a variable without missing values)
> divided by a constant $b, all this is accumulated in $denominador,
> previously executed the subroutine "kernel" (explained down), all this is
> realized for each element of prof_val (called $long and inserted as
> parameter). In the same way a calculus will be accumulated in $numerador
> where the value of the sub "kernel" is multiplied by the value of the
> variable without missing value. At the end I will obtain a quotient.
>
> In addition to, the subroutine "kernel" only do a simple mathematical
> calculus and gives a number to "suavizar".
>
> I think I have troubles to read $long or parameters from other sub's,
> there is a ilegal division of numerador/denomimador because there aren't
> something assigned to them.
>
> I have checked some web pages and it's difficult to find examples like
> this where there is manipulations of matrices or sub's inside others sub's.
>
> I attach the script if you would like to read and correct. Sorry for my
> english and thanks a lot!!
> #!/fs/pkgs/share/perl/bin/perl -w
You should enable strict as well
use strict;
> for ($i=1; $i<=7; ++$i) # y en caso de ser mas o menos columnas, hay que estar
> sustituyendo aca ????????????
> {
> $j[$i] = 0; $j1[$i] = 0;
> }
You do realize that arrays in perl start at zero and not one?
my @j = my @j1 = ( undef, (0) x 7 );
> while (<LAS>) # Lectura de todas las lineas
> {
> $linea = $_;
> if ($linea=~ /^\s*[1-9]/)
> {
> @tmp = split (/\s+/, $linea); # Recorta la linea en el # de columnas
> existentes
Why copy $_ to $linea? Do you not consider 0 would be valid in this
situation? You are basing your usage of @j, @j1 and @tmp on the fact
that split( /\s+/ ) assigns an empty string to the first element of @tmp
however if you use the correct form of split this wouldn't happen. Also
it looks like you need to test for negative numbers as well.
while ( my $linea = <LAS> ) # Lectura de todas las lineas
{
if ( $linea =~ /^\s*-?[1-9]/ )
{
my @tmp = split ' ', $linea; # Recorta la linea en el # de
columnas existentes
Or without $linea:
while ( <LAS> ) # Lectura de todas las lineas
{
if ( /^\s*-?[1-9]/ )
{
my @tmp = split; # Recorta la linea en el # de columnas
existentes
Or without the large if block:
while ( <LAS> ) # Lectura de todas las lineas
{
next unless /^\s*-?[1-9]/;
my @tmp = split; # Recorta la linea en el # de columnas existentes
> print ("Introduzca el ancho de banda (b): ");
> $b = <STDIN>;
> chop ($b);
You should use chomp instead of chop.
> if ($b > 0)
> {
> printf (SAL " Prof Obj Intensidad RT\n");
> guardar([EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], 2, $long_rt);
>
> printf (SAL " Prof Obj Intensidad VCL\n");
> ...
You should be using print instead of printf.
> sub suavizar # el ($$@@) es opcional
> {
> #$prof_obj = $_[0];
> #$b = $_[1];
> [EMAIL PROTECTED] = $_[2];
> [EMAIL PROTECTED] = $_[3];
> #$long = $_[4];
> #($prof_obj, $b, @$prof_val, @$valor, $long) = (shift, shift, shift, shift); # Es
> equivalente a lo de arriba
> ($prof_obj, $b, @$prof_val, @$valor, $long) = @_; #se puede sustituir por la
> linea arriba
When you assign a list to an array, the array will be assigned the whole
list. If you have any variables after the array they will not be
assigned any data.
$prof_obj will be assigned the value of $_[0]
$b will be assigned the value of $_[1]
@$prof_val will be assigned the rest of the values in @_
@$valor will not be assigned anything
$long will not be assigned anything
perldoc perlsub
> local ($u, $j2, $denominador, $numerador);
You should use my instead of local.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]