Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

prob keeping my floats floating

by perl-diddler (Chaplain)
on Feb 15, 2019 at 01:48 UTC ( [id://1229926]=perlquestion: print w/replies, xml ) Need Help??

perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

Having a problem keeping my floating point vars as floats and not being truncated to integers when I try to split the string Lemme show working vs. non:
#working: >perl -e 'printf "(%s)\n", $ARGV[0]', 2.3 (2.3) #still good: > perl -e 'printf "%s\n", do { $ARGV[0] }', 2.3 (2.3) # now to split > perl -we 'printf("(%s)\n", split(q(.), "$ARGV[0]" ) );' 2.3 Missing argument in printf at -e line 1. ()
Why?

Why missing (argument), and why empty output?
split should output an array in list context and the list length in scalar context, so either a list (array ref, I guess?) of "2, 3" or a scalar '2', as number of elements, no?

What's tripping things up? Ideas?

Replies are listed 'Best First'.
Re: prob keeping my floats floating
by pryrt (Abbot) on Feb 15, 2019 at 02:15 UTC

    split expects a pattern; . is special to a pattern:

    C:\Users\Peter>perl -we "print join ':', split('.', '2.3.4')" C:\Users\Peter>perl -we "print join ':', split('\.', '2.3.4')" 2:3:4

    addendum: showing number of elements:

    C:\Users\Peter>perl -we "print scalar split('.', '2.3.4', -1)" 6 C:\Users\Peter>perl -we "print scalar split('\.', '2.3.4', -1)" 3
      Yeah, I thought I had a backslash before it. I fixed it a different way by changing the 1st arg to an explicit pattern: (this one shows the array ref):
      > perl -we ' use P; my @x = split( m{\.}, "$ARGV[0]"); P "\nargv0=%s", "$ARGV[0]"; P "(%s)\n", \@x' "2.3" argv0=2.3 ([2, 3])
      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1229926]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found