CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Issue with Array Initialization?

 



EverGreen1
New User

Aug 12, 2016, 10:30 AM

Post #1 of 6 (2678 views)
Issue with Array Initialization? Can't Post

I'm new to perl with previous experience using c++, c, Matlab, VBA, and some others I don't use often. I'm trying to write a program that simulates information coming from an AC measuring device but I can't seem to get the syntax for an array correct.

It says there's a syntax error at line 13.
@CurrentWave[$inc] = $Wave;

Thank you for any and all help.

Code:



Code
#!/usr/bin/perl -w 
#############################################
# Generating a Sine Wave
#############################################
use strict;
{
#Set entries of the command line to the following set.
my ($Amplitude, $Frequency, $Phase) = @ARGV;
my $inc = 0;
my @CurrentWave;
for (my $Time = 0.000; $Time <1.000; $Time = $Time + 0.001){
#Define the Current waveform from CT.
my $Wave = $Amplitude * sin((2 * 3.14159 * $Frequency * $Time) + $Phase);
@CurrentWave[$inc] = $Wave;
$inc = $inc + 1;
print @CurrentWave($inc);
}
#Define the RMS
my $RMSCurrent = $Amplitude/(sqrt(2));
}



BillKSmith
Veteran

Aug 12, 2016, 1:24 PM

Post #2 of 6 (2672 views)
Re: [EverGreen1] Issue with Array Initialization? [In reply to] Can't Post

You have three problems.

In your code, you have ($inc) rather than [$inc].

You increment $inc before the print.

You must add a newline to your print.


Code
    $CurrentWave[$inc] = $Wave; 
print $CurrentWave[$inc], "\n";
$inc = $inc + 1;
}

Good Luck,
Bill

(This post was edited by FishMonger on Aug 12, 2016, 1:24 PM)


FishMonger
Veteran / Moderator

Aug 12, 2016, 1:27 PM

Post #3 of 6 (2669 views)
Re: [BillKSmith] Issue with Array Initialization? [In reply to] Can't Post


Quote
You have three problems.

I'd say four problems. The fourth one being the incorrect/inconsistent code formatting/indentation.


(This post was edited by FishMonger on Aug 12, 2016, 1:28 PM)


EverGreen1
New User

Aug 12, 2016, 2:10 PM

Post #4 of 6 (2663 views)
Re: [BillKSmith] Issue with Array Initialization? [In reply to] Can't Post

Thanks to the both of you for your answers.

After thinking about it, I realized I was trying to assign scalar values to a set variable. If I tell perl that @CurrentWave is a variable I don't have to do it again in the loop; I can treat it as a scalar, not a set/vector.

Thanks.


Code
#!/usr/bin/perl -w 
##############################################################################
# Generating a Sine Wave
##############################################################################
use strict;
{
#Set entries of the command line to the following set.
my ($Amplitude, $Frequency, $Phase) = @ARGV;
my $inc = 0;
my @CurrentWave;
unless(defined $Amplitude){ # If there's nothing typed in the terminal line.
$Amplitude = 120; # Set the alarm priority to 10. this will cover all priorities.
}
unless(defined $Frequency){ # If there's nothing typed in the terminal line.
$Frequency = 60; # Set the alarm priority to 10. this will cover all priorities.
}
unless(defined $Phase){ # If there's nothing typed in the terminal line.
$Phase = 0; # Set the alarm priority to 10. this will cover all priorities.
}
for (my $Time = 0.000; $Time <1.000; $Time = $Time + 0.001){
#Define the Current waveform from CT.
my $Wave = $Amplitude * sin((2 * 3.14159 * $Frequency * $Time) + $Phase);
$CurrentWave[$inc] = $Wave;
print $CurrentWave[$inc], "\n";
$inc = $inc + 1;
}
#Define the RMS
my $RMSCurrent = $Amplitude/(sqrt(2));
}



BillKSmith
Veteran

Aug 13, 2016, 7:37 AM

Post #5 of 6 (2639 views)
Re: [EverGreen1] Issue with Array Initialization? [In reply to] Can't Post

The array @CurrentWave is not needed at all. You could print $Wave. The outermost set of braces is not needed.

Your new code is much better, but the style is still more "c" than perl. The following code assumes that you do need @CurrentWave for later processing.


Code
use strict; 
use warnings;
use English;
use Math::Trig ':pi';

my ($Amplitude, $Frequency, $Phase) = @ARGV;
$Amplitude //= 120; # Volts
$Frequency //= 60; # hz
$Phase //= 0; # radians
my $w = 2 * pi * $Frequency; # Angular rate (radians/sec)
my $RMSCurrent = $Amplitude / sqrt(2);

my @CurrentWave
= map {
my $Time = $_/1000;
$Amplitude * sin($w * $Time + $Phase)
} (0..999);

do{
local $LIST_SEPARATOR = "\n";
print "@CurrentWave\n"
};


Note the use of the module to get the constant "pi". It is both clearer and more accurate than your litteral. I could have saved the multiply by two if I had imported pi2 instead, but I prefer it this way.

The assignment version of the "defined or" operator handles the arguments exactly the way you intend. For production code, it would be even better to use one of the Getopt modules.

The function 'map' builds the array. it handles all the iteration and storage details.

The special variable $LIST_SEPARATOR ($") allows us to print the entire array with one statement. The do and local are not really needed. They are used here to restrict the scope of $LIST_SEPARATOR to that print statement.
Good Luck,
Bill


EverGreen1
New User

Aug 22, 2016, 10:54 AM

Post #6 of 6 (2519 views)
Re: [BillKSmith] Issue with Array Initialization? [In reply to] Can't Post

Thank you for your reply. This is much clearer.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives