ใน Perl นี้สามารถทำได้ดังนี้
#!/usr/bin/perl
#create a line of arbitrary data
$line = "1 2 3 4 5";
# splt the line into an array (we call the array 'array', for lolz)
@array = split(' ', $line);
# print the last element in the array, followed by a newline character;
print "$array[-1]\n";
เอาท์พุท:
$ perl last.pl
5
$
นอกจากนี้คุณยังสามารถวนซ้ำไฟล์เป็นสคริปต์ตัวอย่างที่ฉันเขียนเพื่อแยกไฟล์ที่เรียกว่า budget.dat
ข้อมูลตัวอย่างใน budget.dat:
Rent              500
Food              250
Car               300
Tax               100
Car Tax           120
Mag Subscription  15
(คุณสามารถเห็นฉันต้องการจับเฉพาะคอลัมน์ "สุดท้าย" ไม่ใช่คอลัมน์ 2 เท่านั้น)
บท:
#!/usr/bin/perl
$budgetfile = "budget.dat";
open($bf, $budgetfile)
        or die "Could not open filename: $filename $!";
print "-" x 50, "\n";
while ( $row = <$bf> ) {
        chomp $row;
        @r = split (' ', $row);
        print "$row ";
        $subtotal += $r[-1];
        print "\t$subtotal\n";
}
print "-" x 50, "\n";
print "\t\t\t Total:\t$subtotal\n\n";