PDL::Char(3pm)

πŸ’– PDL::Char β€” the perl manual page, but make it cute πŸ’–

ok so bestie, lemme tell you about PDL::Char, which is a PDL subclass that lets you read and write fixed-length character strings as byte PDLs. it's giving "strings but make it arrays" and honestly it ate. this is part of User Contributed Perl Documentation, iykyk πŸ’…


✨ SYNOPSIS ✨

use PDL;
use PDL::Char;

my $pchar = PDL::Char->new( [['abc', 'def', 'ghi'],['jkl', 'mno', 'pqr']] );

$pchar->setstr(1,0,'foo');

print $pchar; # 'string' bound to "", perl stringify function
# Prints:
# [
#  ['abc' 'foo' 'ghi']
#  ['jkl' 'mno' 'pqr']
# ]

print $pchar->atstr(2,0);
# Prints:
# ghi

🌸 DESCRIPTION 🌸

so basically this subclass of PDL lets you manipulate PDLs of 'byte' type as if they were made of fixed length strings, not just numbers. no cap, that's the whole vibe πŸ¦‹

this kind of behavior is highkey useful when you wanna work with character grids. and here's the important part bestie: the indexing is done on a string level and not a character level for the 'setstr' and 'atstr' commands. periodt.

this module is in particular useful for writing NetCDF files that include character data using the PDL::NetCDF module. obsessed πŸŽ€


πŸ”₯ FUNCTIONS πŸ”₯

🌈 new

function to create a byte PDL from a string, list of strings, list of list of strings, etc. she's flexible, she's iconic πŸ‘‘

# create a new PDL::Char from a perl array of strings
$strpdl = PDL::Char->new( ['abc', 'def', 'ghij'] );

# Convert a PDL of type 'byte' to a PDL::Char
$strpdl1 = PDL::Char->new (sequence (byte, 4, 5)+99);

$pdlchar3d = PDL::Char->new([['abc','def','ghi'],['jkl', 'mno', 'pqr']]);

πŸ¦‹ string

function to print a character PDL (created by 'char') in a pretty format. she's the main character of formatting, fr fr ✨

$char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
print $char; # 'string' bound to "", perl stringify function
# Prints:
# [
#  ['abc' 'def' 'ghi']
#  ['jkl' 'mno' 'pqr']
# ]

# 'string' is overloaded to the "" operator, so:
# print $char;
# should have the same effect.

πŸ’… setstr

function to set one string value in a character PDL. the input position is the position of the string, not a character in the string. the first dimension is assumed to be the length of the string. okay so keep up πŸ‘€

the input string will be null-padded if the string is shorter than the first dimension of the PDL. it will be truncated if it is longer. no drama, she just does it.

$char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
$char->setstr(0,1, 'foobar');
print $char; # 'string' bound to "", perl stringify function
# Prints:
# [
#  ['abc' 'def' 'ghi']
#  ['foo' 'mno' 'pqr']
# ]
$char->setstr(2,1, 'f');
print $char; # 'string' bound to "", perl stringify function
# Prints:
# [
#  ['abc' 'def' 'ghi']
#  ['foo' 'mno' 'f']      -> note that this 'f' is stored "f\0\0"
# ]

πŸ“ atstr

function to fetch one string value from a PDL::Char type PDL, given a position within the PDL. the input position is the position of the string, not a character in the string. the length of the input string is the implied first dimension. she's setstr's ✨glow-up sibling✨ πŸ’œ

$char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
print $char->atstr(0,1);
# Prints:
# jkl

🫢 quick reminder bestie: the input position for both setstr and atstr is a string index, not a character index. literal character-level indexing is not what's happening here, and that's on purpose. slay.

πŸ’– the cute lil sign-off πŸ’–

this page is perl v5.42.3, dated 2026-08-20, and it's the PDL::Char(3pm) manual β€” go forth and manipulate your fixed-length strings with main character energy, bestie, you got this πŸŒŸπŸ’…πŸ§šπŸ’œ

πŸ“¦ where this page comes from