Data processing is one of the most common and powerful uses of Perl. Perl's text manipulation capabilities, combined with its extensive library of modules, make it an excellent choice for tasks such as parsing, transforming, and analyzing data. In this section, we will cover the following topics:

  1. Reading and Writing Data
  2. Data Parsing Techniques
  3. Data Transformation
  4. Data Analysis

  1. Reading and Writing Data

Reading Data from Files

To read data from a file in Perl, you can use the open function. Here is a simple example:

# Open a file for reading
open(my $fh, '<', 'data.txt') or die "Could not open file 'data.txt' $!";

# Read the file line by line
while (my $line = <$fh>) {
    chomp $line;  # Remove newline character
    print "$line\n";
}

# Close the file handle
close($fh);

Writing Data to Files

To write data to a file, you can also use the open function, but with a different mode:

# Open a file for writing
open(my $fh, '>', 'output.txt') or die "Could not open file 'output.txt' $!";

# Write some data to the file
print $fh "This is a line of text\n";

# Close the file handle
close($fh);

Practical Exercise

Exercise: Write a Perl script that reads a file named input.txt, converts all text to uppercase, and writes the result to a file named output.txt.

Solution:

# Open the input file for reading
open(my $in_fh, '<', 'input.txt') or die "Could not open file 'input.txt' $!";

# Open the output file for writing
open(my $out_fh, '>', 'output.txt') or die "Could not open file 'output.txt' $!";

# Process each line
while (my $line = <$in_fh>) {
    chomp $line;
    $line = uc($line);  # Convert to uppercase
    print $out_fh "$line\n";
}

# Close the file handles
close($in_fh);
close($out_fh);

  1. Data Parsing Techniques

Parsing CSV Files

CSV (Comma-Separated Values) files are a common format for data exchange. Perl has a module called Text::CSV that makes it easy to parse CSV files.

use Text::CSV;

# Create a new CSV parser
my $csv = Text::CSV->new({ binary => 1 });

# Open the CSV file
open(my $fh, '<', 'data.csv') or die "Could not open file 'data.csv' $!";

# Read and parse each line
while (my $row = $csv->getline($fh)) {
    my ($col1, $col2, $col3) = @$row;
    print "Column 1: $col1, Column 2: $col2, Column 3: $col3\n";
}

# Close the file handle
close($fh);

Parsing JSON Data

JSON (JavaScript Object Notation) is another popular data format. Perl's JSON module can be used to parse JSON data.

use JSON;

# JSON string
my $json_text = '{"name": "John", "age": 30, "city": "New York"}';

# Decode JSON to Perl data structure
my $data = decode_json($json_text);

# Access data
print "Name: $data->{name}\n";
print "Age: $data->{age}\n";
print "City: $data->{city}\n";

Practical Exercise

Exercise: Write a Perl script that reads a CSV file named data.csv and prints each row in JSON format.

Solution:

use Text::CSV;
use JSON;

# Create a new CSV parser
my $csv = Text::CSV->new({ binary => 1 });

# Open the CSV file
open(my $fh, '<', 'data.csv') or die "Could not open file 'data.csv' $!";

# Read and parse each line
while (my $row = $csv->getline($fh)) {
    my $json_text = encode_json($row);
    print "$json_text\n";
}

# Close the file handle
close($fh);

  1. Data Transformation

Transforming Data with Perl

Data transformation involves converting data from one format or structure to another. Perl's powerful text manipulation functions make it ideal for this task.

# Sample data
my $data = "name: John, age: 30, city: New York";

# Transform data to JSON format
$data =~ s/(\w+): (\w+)/"$1": "$2"/g;
$data = "{$data}";

print "$data\n";

Practical Exercise

Exercise: Write a Perl script that reads a file named data.txt containing key-value pairs (e.g., name: John), transforms the data into JSON format, and writes the result to a file named data.json.

Solution:

use JSON;

# Open the input file for reading
open(my $in_fh, '<', 'data.txt') or die "Could not open file 'data.txt' $!";

# Open the output file for writing
open(my $out_fh, '>', 'data.json') or die "Could not open file 'data.json' $!";

# Read and transform data
my %data;
while (my $line = <$in_fh>) {
    chomp $line;
    my ($key, $value) = split(/: /, $line);
    $data{$key} = $value;
}

# Convert to JSON and write to file
print $out_fh encode_json(\%data);

# Close the file handles
close($in_fh);
close($out_fh);

  1. Data Analysis

Analyzing Data with Perl

Perl can be used to perform various data analysis tasks, such as statistical analysis, data summarization, and more.

# Sample data
my @data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

# Calculate the sum
my $sum = 0;
$sum += $_ for @data;

# Calculate the mean
my $mean = $sum / @data;

print "Sum: $sum\n";
print "Mean: $mean\n";

Practical Exercise

Exercise: Write a Perl script that reads a file named numbers.txt containing a list of numbers, one per line, calculates the sum and mean of the numbers, and prints the results.

Solution:

# Open the input file for reading
open(my $fh, '<', 'numbers.txt') or die "Could not open file 'numbers.txt' $!";

# Read and process data
my @numbers;
while (my $line = <$fh>) {
    chomp $line;
    push @numbers, $line;
}

# Calculate the sum
my $sum = 0;
$sum += $_ for @numbers;

# Calculate the mean
my $mean = $sum / @numbers;

print "Sum: $sum\n";
print "Mean: $mean\n";

# Close the file handle
close($fh);

Conclusion

In this section, we covered various aspects of data processing with Perl, including reading and writing data, parsing different data formats, transforming data, and performing basic data analysis. These skills are essential for any Perl programmer working with data. In the next section, we will explore Perl's capabilities in network programming.

© Copyright 2024. All rights reserved