in reply to Re: Reading into array with sscanf
in thread Reading into array with sscanf

I tried your example and it works BUT it does not do the same as my code example. My code fragments return values into the c_speed array that are *numeric* not the strings that split returns, Which is why I was using sscanf from String::Scanf The variable array c_speed needs to be numeric not string because the values get processed elsewhere.

Replies are listed 'Best First'.
Re^3: Reading into array with sscanf
by Corion (Patriarch) on Jul 16, 2024 at 13:27 UTC

    Perl makes very little difference between strings and numbers.

    There are Perl operators, like + that treat their arguments as numbers, and other operators that treat their arguments as strings.

    Where in your code do you see that the variables get treated differently?

Re^3: Reading into array with sscanf
by hippo (Archbishop) on Jul 16, 2024 at 13:44 UTC

    They perform just fine in a numeric context.

    #!/usr/bin/env perl use strict; use warnings; my $save_fn = 'colintu.dat'; my @c_speed = (5, 8, 12, 8); open my $sfh, '>', $save_fn or die "Unable to open file $save_fn : $!"; print $sfh "@c_speed\n"; close $sfh; open my $lfh, '<', $save_fn or die "Unable to open file $save_fn : $!"; @c_speed = split / /, <$lfh>; print "Have: @c_speed\n"; @c_speed = map { $_ * 2 } @c_speed; print "Doubled: @c_speed\n"; close $lfh; unlink $save_fn; # clean up

    🦛