S'rCat
S'rCat

Reputation: 638

perl split function returns infinite array

I think there's a bug here. I have a string consisting of numbers separated by ':'. when I use split(), the number of list items is not a number, it returns "1289,2235,2300,2336". What's wrong here?

#!/opt/local/bin/perl

$data="10:2284:2345:2381:9:2235:2300:2336:8:2212:2273:2320:7:2194:2262:2295:6:2165:2232:2269:5:2118:2167:2205:4:2086:2142:2161:3:2039:2106:2138:2:2034:2088:2127:1:2028:2079:2109:01:1999:2046:2080:02:1972:2016:2052:03:1960:1987:2019:04:1915:1945:1971:05:1870:1888:1911:06:1798:1828:1855:07:1764:1789:1809:08:1692:1728:1753:09:1665:1688:1706:010:1636:1657:1679:011:1575:1607:1641:012:1549:1582:1620:013:1485:1539:1582:014:1395:1485:1540:015:1382:1456:1504:016:1368:1422:1465:017:1301:1360:1405:018:1267:1252:1283:019:1213:1252:1283:020::1159:1180:021::1112:1143:022::1087:1094::";

@l = split(':',$data);

print scalar @l ;

Upvotes: 0

Views: 71

Answers (1)

Sobrique
Sobrique

Reputation: 53478

Your code works fine for me - I get 128 printed when I run it. I think the thing that will be tripping you up is that you don't print a line feed. E.g. \n or use say.

So your number is:1289,2235,2300,2336 which implies this bit of the code is printing 128 and elsewhere in your code is printing 9,2235,2300,2336.

If you can extract more of your code, and produce an MCVE - minimal complete verifiable example - the fine folk of StackOverflow can help you further. I suspect when you do this, your problem will disappear - whilst there are bugs in perl, it's quite a mature language and so the bugs really don't show up unless you're doing something particularly obscure.

Upvotes: 5

Related Questions