Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mysterytramp

macrumors 65816
Original poster
Jul 17, 2008
1,334
4
Maryland
This script doesn't do anything, but I'm trying to get it to do it well.

It accepts arguments. If it has two arguments, it should say which is first and which is second. If it has just one argument, it should just print the first argument and print that the second is nonexistent. And if there are no arguments, it should say they are both nonexistent.

Instead, I get these errors:

$ perl argnames2.plx arg1 arg2
Use of uninitialized value in concatenation (.) or string at argnames2.plx line 36.
First file was
Use of uninitialized value in concatenation (.) or string at argnames2.plx line 37.
Second file was
The number of items was 2
$

Here's the script:

Code:
#!/usr/bin/perl
# matchtest.plx
use warnings;
use strict;

my $scalar1;
my $firstFile;
my $secondFile;
my @array1;

@array1 = @ARGV;

$scalar1 = @array1;

if ($scalar1 == 2) {

	my $firstFile = shift @array1;
	my $secondFile = shift @array1;

	} else {

		if ($scalar1 == 1) {

			my $firstFile = shift @array1;
			my $secondFile = "nonexistent";
	
	} else {

	my $firstFile = "nonexistent";
	my $secondFile = "nonexistent";

	}
}


print "First file was $firstFile\n";
print "Second file was $secondFile\n";
print "The number of items was $scalar1\n";

The errors are upset with the first two print statements. Why aren't they legit?

mt
 
This script doesn't do anything, but I'm trying to get it to do it well.

It accepts arguments. If it has two arguments, it should say which is first and which is second. If it has just one argument, it should just print the first argument and print that the second is nonexistent. And if there are no arguments, it should say they are both nonexistent.

Instead, I get these errors:



Here's the script:

Code:
#!/usr/bin/perl
# matchtest.plx
use warnings;
use strict;

my $scalar1;
my $firstFile;
my $secondFile;
my @array1;

@array1 = @ARGV;

$scalar1 = @array1;

if ($scalar1 == 2) {

	my $firstFile = shift @array1;
	my $secondFile = shift @array1;

	} else {

		if ($scalar1 == 1) {

			my $firstFile = shift @array1;
			my $secondFile = "nonexistent";
	
	} else {

	my $firstFile = "nonexistent";
	my $secondFile = "nonexistent";

	}
}


print "First file was $firstFile\n";
print "Second file was $secondFile\n";
print "The number of items was $scalar1\n";

The errors are upset with the first two print statements. Why aren't they legit?

mt

When you use my in the blocks you declare new scalars with the same name as the outermost scalars. That's the bug.

Also, the indentation is weird, and you should really use elsif instead of if within an else. I would change it to

Code:
#!/usr/bin/perl
# matchtest.plx
use warnings;
use strict;

my $scalar1;
my $firstFile;
my $secondFile;
my @array1;

@array1 = @ARGV;

$scalar1 = @array1;

if ($scalar1 == 2) {
  $firstFile = shift @array1;
  $secondFile = shift @array1;
} elsif ($scalar1 == 1) {
  $firstFile = shift @array1;
  $secondFile = "nonexistent";
} else {
  $firstFile = "nonexistent";
  $secondFile = "nonexistent";
}

print "First file was $firstFile\n";
print "Second file was $secondFile\n";
print "The number of items was $scalar1\n";

but I think I would do it like this:

Code:
#!/usr/bin/perl
# matchtest.plx
use warnings;
use strict;

my $length = @ARGV;
my $firstFile = "nonexistent";
my $secondFile = "nonexistent";

$firstFile = shift @ARGV if @ARGV;
$secondFile = shift @ARGV if @ARGV;

print "First file was $firstFile\n";
print "Second file was $secondFile\n";
print "The number of items was $length\n";

Bear in mind that I haven't coded Perl in years and I generally try to avoid it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.