in reply to Reading into array with sscanf
The correct answer is to choose an appropriate data format to save as, such as CSV, JSON, XML, etc. You can then use your module of choice for reliably reading and writing. If it's just for temporary backing storage then Storable, Sereal and friends will do the job too.
However, for your example set of integers here's a workable approach using split:
#!/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"; close $lfh; unlink $save_fn; # clean up
(Edited for typo fix - thanks, Corion)
🦛
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading into array with sscanf
by colintu (Acolyte) on Jul 16, 2024 at 11:38 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 16, 2024 at 12:34 UTC | |
by LanX (Saint) on Jul 16, 2024 at 12:27 UTC | |
|
Re^2: Reading into array with sscanf
by colintu (Acolyte) on Jul 16, 2024 at 13:08 UTC | |
by Corion (Patriarch) on Jul 16, 2024 at 13:27 UTC | |
by hippo (Archbishop) on Jul 16, 2024 at 13:44 UTC |