ตัวแปรพื้นฐานในภาษา Perl
ในภาษา Perl มีตัวแปรเพียงไม่กี่ชนิด มีประเภทใหญ่ๆ ดังนี้

ตัวแปรพื้นฐานในภาษา Perl
ในภาษา Perl มีตัวแปรเพียงไม่กี่ชนิด มีประเภทใหญ่ๆ ดังนี้
- Scalar
- Array
- Associative Arrays (Hashes)
การใช้ตัวแปรแบบ Scalar
เป็นตัวแปรที่ใช้สำหรับเก็บข้อมูลเดี่ยวๆ ที่เป็น Integer หรือ String การกำหนดตัวแปรประเภทนี้ไม่จำเป็นต้องประกาศตัวแปรก่อน โดยสามารถใช้ได้เลย ชื่อของตัวแปรประเภทนี้ถูกนำด้วยเครื่องหมาย dollar ($) ให้ศึกษาจากตัวอย่างต่อไปนี้
#!/usr/local/bin/perl
$myname = "Tawatchai Iempairote";
$myage = 28;
$myoffice = "Computer Science Dept.";
print "I am $myname, now my age is $myage ";
print "I work at $myoffice ..
";
การใช้ตัวแปรแบบ Array
เป็นตัวแปรที่ใช้สำหรับเก็บข้อมูลเป็นชุด หรือเป็นแถวเรียงกัน ชื่อของตัวแปรจะถูกนำด้วยเครื่องหมาย @ ให้ศึกษาจากตัวอย่างต่อไปนี้
# Next I will show how to use array variables
@mycourse = ("310211","310213","310417","315425","310303","310101","310201");
print "This semester I teach the following courses ";
print " $mycourse[0] ";
print " $mycourse[1] ";
print " $mycourse[4] ";
print "In fact I teach all courses : @mycourse
";
การใช้ตัวแปรแบบ Associative Array
เป็นตัวแปรที่ใช้สำหรับเก็บข้อมูลแบบ array ที่มีข้อมูลในแต่ละ element สัมพันธ์กันเป็นคู่ ชื่อของตัวแปรจะถูกนำด้วยเครื่องหมาย % ให้ศึกษาจากตัวอย่าง ต่อไปนี้
# Also I will show you here how to use Associative array
%course = ("310211","Computer Science I","310213","Computer Science II", "310417
","Selected Topics","315425","Object Oriented Analysis","310303","Computer in Bu
siness");
print "The course name are : %course ";
print "I love to teach $course{'310213'} ";
print "and $course{'315425'} "
ผลจากการ run โปรแกรมตัวอย่างแสดงชนิดข้อมูลทั้ง 3 แบบข้างต้น
I am Tawatchai Iempairote, now my age is 28
I work at Computer Science Dept. ..
This semester I teach the following courses
310211
310213
310303
In fact I teach all courses : 310211 310213 310417 315425 310303 310101 310201
The course name are : %course
I love to teach Computer Science II
and Object Oriented Analysis
ไอทีจีเนียส เอ็นจิเนียริ่ง ให้บริการด้านไอทีครบวงจร
ทั้งงานอบรม (Training) สัมมนา รับเขียนโปรแกรม เว็บไซต์ แอปพลิเคชัน งานออกแบบกราฟิก และงานด้าน E-Marketing ทั้ง SEO, PPC และ Social Media Marketing
ติดต่อเราเพื่อสอบถามผลิตภัณฑ์ ขอราคา หรือปรึกษาเรื่องไอทีได้เลยค่ะ
