perl how to split string from array and save it back
Categories:
Perl: Splitting Strings from Arrays and Reassembling Data

Learn how to effectively split string elements within a Perl array, process the resulting parts, and save the modified data back into a structured format. This article covers common scenarios and best practices.
In Perl programming, it's a common task to manipulate data stored in arrays. Often, individual elements within an array are strings that contain multiple pieces of information separated by a delimiter. This article will guide you through the process of splitting these strings, performing operations on the resulting parts, and then storing the modified data back into an array or another suitable data structure.
Understanding the split Function
The split function is a fundamental tool in Perl for breaking a string into a list of substrings. It takes a regular expression as a delimiter and the string to be split. The function returns a list of the substrings, which can then be assigned to an array or used directly.
my @data_strings = (
"apple:red:fruit",
"banana:yellow:fruit",
"carrot:orange:vegetable"
);
foreach my $item_string (@data_strings) {
my @parts = split(/:/, $item_string);
print "Original: $item_string\n";
print "Parts: @parts\n\n";
}
Basic usage of the split function to break strings by a colon delimiter.
split is called with an empty string as the delimiter (split(//, $string)), it splits the string into individual characters. If no delimiter is provided, it splits on whitespace.Processing and Reassembling Data
After splitting a string, you'll typically want to process the individual parts. This might involve modifying them, selecting specific parts, or combining them with other data. The goal is often to transform the original flat string into a more structured format, such as an array of arrays (AoA) or an array of hashes (AoH), which are easier to work with programmatically.

Workflow for splitting, processing, and reassembling string data.
my @raw_data = (
"Name:Alice;Age:30;City:New York",
"Name:Bob;Age:25;City:London",
"Name:Charlie;Age:35;City:Paris"
);
my @structured_data;
foreach my $record_string (@raw_data) {
my %record_hash;
my @fields = split(/;/, $record_string);
foreach my $field (@fields) {
my ($key, $value) = split(/:/, $field, 2); # Limit split to 2 parts
$record_hash{$key} = $value;
}
push @structured_data, { %record_hash }; # Store as hash reference
}
# Print structured data for verification
foreach my $record_ref (@structured_data) {
print "Name: $record_ref->{'Name'}, Age: $record_ref->{'Age'}, City: $record_ref->{'City'}\n";
}
Splitting strings into key-value pairs and storing them as an array of hash references.
LIMIT argument in split. If your delimiter might appear within a value (e.g., key:value:with:colon), using a limit like split(/:/, $string, 2) can prevent unintended splitting of the value part.Practical Example: Parsing CSV-like Data
A common use case for splitting strings is parsing CSV (Comma Separated Values) or similar delimited data. While dedicated CSV modules exist (like Text::CSV_XS), understanding the manual approach with split is valuable for simpler cases or when you need fine-grained control.
my @csv_lines = (
"ID,Product,Price,Quantity",
"101,Laptop,1200.50,5",
"102,Mouse,25.00,20",
"103,Keyboard,75.99,10"
);
my @products;
my @header = split(/,/, shift @csv_lines); # Get header and remove from array
foreach my $line (@csv_lines) {
my @values = split(/,/, $line);
my %product_info;
for my $i (0 .. $#header) {
$product_info{$header[$i]} = $values[$i];
}
push @products, { %product_info };
}
print "\nParsed Products:\n";
foreach my $product_ref (@products) {
print "Product: $product_ref->{'Product'}, Price: $product_ref->{'Price'}, Quantity: $product_ref->{'Quantity'}\n";
}
Parsing CSV-like data from an array of strings into an array of hash references, using the first line as headers.
Text::CSV_XS module from CPAN. It handles edge cases much more reliably than a simple split.