defined

can someone give me your hand
i'm a newcomer to perl
#!/usr/bin/perl -w

$in = '';
@array1 = ();
@array2 = ();
@final = ();

print 'Enter the first array: ';
chomp($in=);
@array1 = split('',$in);

#print "@array1
";
#print scalar(@array1);

print 'Enter the second array: ';
chomp($in=);
@array2 = split('',$in);

foreach $el (@array1)
{
foreach $el2 (@array2)
{
if(defined $el2 && $el eq $el2)#what is defined for?
{
$final[$#final+1] = $el; #$final[$#final+1]?
undef $el2; #what is undef for?
last;
}
}
}
print "@final
";

can u give some examples?

Comments

  • How much or little of the posted code are you unclear on? To
    answer your specific questions, 'defined' is a variable test
    that determines whether the variable in question contains a
    legitimate value, or an undef. It will return a boolean true
    or false, depending on the variables contents.

    When you define a variable as:
    [code]
    $in = '';
    @array1 = ();
    @array2 = ();
    @final = ();
    [/code]
    You're setting each variable as undefined.

    undef is a function which also sets a variable as undefined.

    When you have an array, such as @array1, the array will either
    be undefined, or contain slices, or elements. Each slice is
    accessed via the [] brackets. And, just because an array is
    defined, it doesn't mean that it's slices will be defined.
    There are several ways to determine how many slices are in the
    array.
    [code]
    my($arraySize)=@array;
    [/code]
    This sets the variable $arraySize to the count of items in
    the array, meaning it's slices will be from 0 (zero) to
    $arraySize-1
    [code]
    print "Array size is ", scalar @array, "
    ";
    [/code]
    scalar variables will treat the array like a scalar, in this
    case as the number of items in array.
    [code]
    print "Array size is ", $#array+1, "
    ";
    [/code]
    This is my preferred way to handle it. $# on an array will
    return the last slice number of the array. The others are always
    the exact count of items, whereas $# is the last slice number.
    We add one, in this case, to show that.

    In your code, you used -w, which is good, as it gives decent
    warning messages for errors encountered. However, I would
    seriously suggest that you always use:
    [code]
    use strict;
    [/code]
    :in *every* script you write, be it one line or 1000 lines.
    It forces the coder to write cleaner code. IMHO, it is a good
    practice to get into. Here is the comparison script with that
    added in. Notice the differences.
    [code]
    #!/usr/bin/perl
    use strict;
    use warnings; # almost the same thing, in perl 5.6, as -w

    my($in,@array1,@array2,@final); # declaring vars with 'my'
    # sets each to undef

    print 'Enter the first array: ';
    chomp($in=);
    @array1 = split('',$in); # create an array of single chars

    print 'Enter the second array: ';
    chomp($in=);
    @array2 = split('',$in);

    foreach my $el (@array1) {
    foreach my $el2 (@array2) {
    if(defined $el2 && $el eq $el2) {
    $final[$#final+1] = $el;
    undef $el2;
    last;
    }
    }
    }
    print "@final
    ";
    exit;
    [/code]

    I hope this helps! If you have any other questions, please
    feel free to ask!

    TheLinuxDuck
    "Truely you have a dizzying intellect" - The Princess Bride


  • Thanks you TLD
    u are very nice b^0^d

  • : Thanks you TLD
    : u are very nice b^0^d

    I'm not sure what that last part is, but you're welcome. I'm
    just glad to be able to help.


    TheLinuxDuck
    "Truely you have a dizzying intellect" - The Princess Bride


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories