Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Why do i get an extra (duplicate) array element?-

by graff (Chancellor)
on Jun 23, 2016 at 18:41 UTC ( #1166404=note: print w/replies, xml ) Need Help??

in reply to Why do i get an extra (duplicate) array element?-

Based on the last thing you said:

My goal is to somehow stuff all neg values within one array and pos values within another...

I would think you want something like this:

my @pos_array; my @neg_array; while ( my $row = <IN> ) { if ( /(-\d[.\d]+)/ ) { push @neg_array, $1; } elsif ( /(\d[.\d]+)/ ) { push @pos_array $1; } } # now do stuff with those two arrays...
(There are better regex patterns for matching numeric strings, but the above works in the vast majority of cases.)

As for why your script is doing what it does, well, it's hard to say without seeing the actual input data. How about you post your code with data, like this, so we can see what's happening:

#!/usr/bin/perl use strict; use warnings; while ( my $row = <DATA>) { print $row; } __DATA__ 10.00 10.00 -30.00
(Of course, your code and/or data will probably look different -- this is just a demonstration of the idea.)

One last point: substr() returns a scalar, not an array; your code is creating an array with a single element on each loop iteration, and that single element is a string of (up to) 17 characters long. The code doesn't make much sense, actually.

Replies are listed 'Best First'.
Re^2: Why do i get an extra (duplicate) array element?-
by TomDLux (Vicar) on Jun 23, 2016 at 19:18 UTC

    Why not just use the numeric value in the string:

    $ perl -demo DB<1> $A = "123" DB<2> if ( $A < 0 ) { print 'negative' }else {print 'non-negative'} non-negative DB<3> $A = "-123" DB<4> if ( $A < 0 ) { print 'negative' }else {print 'non-negative'} negative

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1166404]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2021-03-04 20:04 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (107 votes). Check out past polls.