I would like to write Perl script to read just first line of a file & calculate the length of second field which is comma separator file.

For example -

expecting result - 4 (character length of second field from just first line i.e. abcd)

Can somebody help me to write perl script for this?

use strict;
use warnings;

my $filename = 'input.csv';
open my $fh, '<', $filename or die "Unable to open $filename: $!";

my $firstline = <$fh>;
print "The first line is $firstline\n";

my @fields = split(/,/, $firstline);
my $len = length($fields[1]);
print "The second field contains '$fields[1]' which has length of $len\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.