notoperlbegineer has asked for the wisdom of the Perl Monks concerning the following question:

How I can add - in between below file. for string we use sustr() function, how about in number i can deal with this Please tell me logic so that I can store all values .

All the below values need to be stored in a variable XYZ which has a field length of 9

$XYZ = @_[8]; 458-43-0764 453-45-3214 462-63-4878 453-99-0002 462-75-5714 631-03-6456 466-91-7461 467-17-2570 454-69-1673 258-61-1036
Kindly email me removed mail address or comment ur answer.

Discipulus put code tags and removed mail address

Replies are listed 'Best First'.
Re: Add - in between number in a file
by poj (Abbot) on Jun 06, 2019 at 15:47 UTC

    I'm guessing you have 9 digit numbers and you want to format them with hyphens. If so then perhaps unpack and join

    #!perl use strict; use warnings; while (<DATA>){ my $str = join '-',unpack 'a3a2a4',$_; printf "%s\n",$str; } __DATA__ 458430764 453453214 462634878 453990002 462755714 631036456 466917461 467172570 454691673 258611036
    poj
Re: Add - in between number in a file
by BillKSmith (Monsignor) on Jun 06, 2019 at 14:46 UTC
    You are confused on several points. In Perl, a 'file' is usually a disc file which we read and write. There is no such function as 'sustr()'. There is no such thing as a variable 'XYZ'. The variable '$XYZ' is a scalar which may contain a number, a single string, or a reference. The variable '@XYZ' is an array which may contain a 'list' of almost anything. You appear to have list of SSN's. If you store them in an array, You can easily add to the end of this list with the function 'push()'.
    use strict; use warnings; my @XYZ = ( '458-43-0764', '453-45-3214', '462-63-4878', '453-99-0002', '462-75-5714', '631-03-6456', '466-91-7461', '467-17-2570', '454-69-1673', '258-61-1036', ); push @XYZ, '111-22-3333'; local $, = "\n"; # Separate fields in output print @XYZ;
    Bill
Re: Add - in between number in a file
by FreeBeerReekingMonk (Deacon) on Jun 08, 2019 at 19:09 UTC
    TIMTOWTDI. Used substr() and simple functions.
    #!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>){ chomp($line); $line = "0" . $line while length($line) < 9; my $first = substr($line, 0, 3); my $second= substr($line, 3, 2); my $third = substr($line, 5, 4); my $str = $first . '-' . $second . '-' . $third; print $str . "\n"; } __DATA__ 458430764 453453214 462634878 453990002 462755714 631036456 466917461 467172570 454691673 258611036 42

    I have added an extra check, just in case it is a smaller number and not the 9 numbers, we put "0" in front of our read $line. but know that Perl handles the numbers in the files as strings, only when you start doing mathematical operations with them, then Perl automatically handles them as numbers. You can also just print a warning and skip, like so:

    if(length($line) != 9){ warn "Bad data read (at line $.): '$line' length is not correct.\n"; next; # skip processing this line } if ($line !~ /^\d+$/){ warn "Bad data read (at line $.): '$line' not numerical.\n"; next; # skip processing this line }

    edit: added latter check. Assuming we have positive integer numbers only (no E or negative numbers, no floats)