Array 다루기 #1

perl 2008. 6. 5. 02:25

 

perl 코드에서 보면, Array를 파라미터로 넘겨서 처리하는 좋은 예제가 있다.

좋은 참조 자료를 바탕으로 재구성하였다.

 

출처 : http://www.webbasedprogramming.com/Perl-5-By-Example/ch5.htm#ExampleUsingtheParameterArray

 

#1

 

 
firstSub(1, 2, 3, 4, 5, 6);
firstSub(1..3);
firstSub("A".."Z");

sub firstSub {
    $numParameters = @_;
    print("The number of parameters is $numParameters\n");
}
This program prints out:
The number of parameters is 6
The number of parameters is 3
The number of parameters is 26

 

 

#2

 

 
areaOfRectangle(2, 3);
areaOfRectangle(5, 6);

sub areaOfRectangle {
    ($height, $width) = @_;

    $area = $height * $width;

    print("The height is $height. The width is $width.
        The area is $area.\n\n");
}
This program prints out:
The height is 2. The width is 3.
        The area is 6.

The height is 5. The width is 6.
        The area is 30.

 

 

 

#3

 
@array = (0..5);
print("Before function call, array = @array\n");
firstSub(@array);
print("After function call, array =  @array\n");

sub firstSub{
    $_[0] = "A";
    $_[1] = "B";
}

This program prints:

Before function call, array =  0 1 2 3 4 5
After function call, array =   A B 2 3 4 5

 

#4

 

@array = (0..5); print("Before function call, array = @array\n"); firstSub(@array); print("After function call, array = @array\n"); sub firstSub{ ($firstVar, $secondVar) = @_; $firstVar = "A"; $secondVar = "B"; }

This program prints:

Before function call, array =  0 1 2 3 4 5
After function call, array =   0 1 2 3 4 5

 

#5

 

@array = (0..5);
print("Before function call, array = @array\n");
firstSub(@array);
print("After function call, array =  @array\n");

sub firstSub{
    ($firstVar, $secondVar) = @_;

    $firstVar = "A";
    $secondVar = "B";
}

This program prints:

Before function call, array =  0 1 2 3 4 5
After function call, array =   0 1 2 3 4 5

 

 

#6

 

$firstVar = 10;
@array    = (0..5);

print("Before function call\n");
print("\tfirstVar = $firstVar\n");
print("\tarray    = @array\n");

firstSub(@array);

print("After function call\n");
print("\tfirstVar = $firstVar\n");
print("\tarray    = @array\n");

sub firstSub{
    ($firstVar, $secondVar) = @_;

    $firstVar = "A";
    $secondVar = "B";
}

This program prints:

Before function call
        firstVar = 10
        array    = 0 1 2 3 4 5

After function call
        firstVar = A
        array    = 0 1 2 3 4 5

 

#6

 

firstSub("AAAAA", "BBBBB");
print("done: firstVar  = $firstVar\n");
print("done: secondVar = $secondVar\n\n");

sub firstSub{
    local ($firstVar) = $_[0];
    my($secondVar)    = $_[1];

    print("firstSub: firstVar  = $firstVar\n");
    print("firstSub: secondVar = $secondVar\n\n");

    secondSub();

    print("firstSub: firstVar  = $firstVar\n");
    print("firstSub: secondVar = $secondVar\n\n");
}

sub secondSub{
    print("secondSub: firstVar  = $firstVar\n");
    print("secondSub: secondVar = $secondVar\n\n");

    $firstVar  = "CCCCC";
    $secondVar = "DDDDD";

    print("secondSub: firstVar  = $firstVar\n");
    print("secondSub: secondVar = $secondVar\n\n");
}

This program prints:

firstSub: firstVar = AAAAA
firstSub: secondVar = BBBBB

secondSub: firstVar  = AAAAA
Use of uninitialized value at test.pl line 19.
secondSub: secondVar =

secondSub: firstVar  = CCCCC
secondSub: secondVar = DDDDD

firstSub: firstVar  = CCCCC
firstSub: secondVar = BBBBB

Use of uninitialized value at test.pl line 3.
done: firstVar  =
done: secondVar = DDDDD

 

 

#7

 
firstSub((0..10), "AAAA");

sub firstSub{
    local(@array, $firstVar) = @_;

    print("firstSub: array    = @array\n");
    print("firstSub: firstVar = $firstVar\n");
}

This program prints:

firstSub: array    = 0 1 2 3 4 5 6 7 8 9 10 AAAA
Use of uninitialized value at test.pl line 8.
firstSub: firstVar =

 

#8

 
firstSub("AAAA", (0..10));

sub firstSub{
    local($firstVar, @array) = @_;

    print("firstSub: array    = @array\n");
    print("firstSub: firstVar = $firstVar\n");
}
This program prints:
firstSub: array    = 0 1 2 3 4 5 6 7 8 9 10
firstSub: firstVar = AAAA

 

 

#9  Array 관련 함수

Function Description
defined(VARIABLE) returns true if VARIABLE has a real value and false if the variable has not yet been assigned a value. This is not limited to arrays, any data type can be checked. Also see the exists function for information about associative array keys.
delete(KEY) Removes the key-value pair from the given associative array. If you delete a value from the %ENV array the environment of the current process is changed, not that of the parent.
each(ASSOC_ARRAY) Returns a two-element list that contains a key and value pair from the given associative array. The function is mainly used so that you can iterate over the associate array elements. A null list is returned when the last element has been read.
exists(KEY) Returns true if the KEY is part of the specified associative array. For instance, exists($array{"Orange"}) returns true if the %array associative array has a key with the value of "Orange."
join(STRING, ARRAY) Returns a string that consists of all of the elements of ARRAY joined together by STRING. For instance, join(">>", ("AA", "BB", "CC")) returns "AA>>BB>>CC".
keys(ASSOC_ARRAY) Returns a list that holds all of the keys in a given associative array. The list is not in any particular order.
map(EXPRESSION, ARRAY) Evaluates EXPRESSION for every element of ARRAY. The special variable $_ is assigned each element of ARRAY immediately before EXPRESSION is evaluated.
pack(STRING, ARRAY) Creates a binary structure, using STRING as a guide, of the elements of ARRAY. You can look in Chapter 8, "References," for more information.
pop(ARRAY) Returns the last value of an array. It also reduces the size of the array by one.
push(ARRAY1, ARRAY2) Appends the contents of ARRAY2 to ARRAY1. This increases the size of ARRAY1 as needed.
reverse(ARRAY) Reverses the elements of a given array when used in an array context. When used in a scalar context, the array is converted to a string, and the string is reversed.
scalar(ARRAY) Evaluates the array in a scalar context and returns the number of elements in the array.
shift(ARRAY) Returns the first value of an array. It also reduces the size of the array by one.
sort(ARRAY) Returns a list containing the elements of ARRAY in sorted order. See Chapter 8, "References," for more information.
splice(ARRAY1, OFFSET, LENGTH, ARRAY2) Replaces elements of ARRAY1 with elements in ARRAY2. It returns a list holding any elements that were removed. Remember that the $[ variable may change the base array subscript when determining the OFFSET value.
split(PATTERN, STRING, LIMIT) Breaks up a string based on some delimiter. In an array context, it returns a list of the things that were found. In a scalar context, it returns the number of things found.
undef(VARIABLE) Slways returns the undefined value. In addition, it undefines VARIABLE which must be a scalar, an entire array or a subroutine name.
unpack(STRING, ARRAY) Does the opposite of pack().
unshift(ARRAY1, ARRAY2) Adds the elements of ARRAY2 to the front of ARRAY1. Note that the added elements retain their original order. The size of the new ARRAY1 is returned.
values(ASSOC_ARRAY) Returns a list that holds all of the values in a given associative array. The list is not in any particular order.

 

 

#10 출력하기

 
%array = ( "100", "Green", "200", "Orange");

while (($key, $value) = each(%array)) {
    print("$key = $value\n");
}
This program prints:
100 = Green
200 = Orange

 

 

#11

 

 

 

 
createPair("100",  "Kathy Jones");
createPair("200",  "Grace Kelly");
createPair("100", "George Orwell");

while (($key, $value) = each %array) {
    print("$key, $value\n");
};

sub createPair{
    my($key, $value) = @_;

    $array{$key} = $value;
};
This program prints
100, George Orwell
200, Grace Kelly

 

#12

 
createPair("100",  "Kathy Jones");
createPair("200",  "Grace Kelly");
createPair("100", "George Orwell");

while (($key, $value) = each %array) {
    print("$key, $value\n");
};

sub createPair{
    my($key, $value) = @_;

    while (defined($array{$key})) {
        $key++;
    }

    $array{$key} = $value;
};
This program prints:
100, Kathy Jones
101, George Orwell
200, Grace Kelly

 

 

 

'perl' 카테고리의 다른 글

Array 쪽 팁  (0) 2008.06.07
Array 다루기 #2  (0) 2008.06.05
Array 다루기 #1  (0) 2008.06.05
Passing Parameters to Functions  (0) 2008.06.05
Perl의 String 관련 함수  (0) 2008.03.22
bash는 float 변수 연산이 안된다.  (0) 2007.10.21
Posted by 김용환 '김용환'

댓글을 달아 주세요