Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Unscalar'ize a fake array

by Marshall (Canon)
on Sep 25, 2017 at 20:26 UTC ( [id://1200070]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Unscalar'ize a fake array
in thread Unscalar'ize a fake array

Well if you just looked at a printout, then this could have been a print out of single scalar text variable containing several lines. To split each line's values into an array, you have to "extract the lines" from the single text variable.

Another fine point once you have extracted the lines...
There are five white space characters, space,\t,\f,\n,\r.

There are 2 ways to split on any of these white space characters.
Perl has a special case, ' 'for the split.
This is the same as the regex /\s+/ which splits on any of the five characters except in how it handles the first potentially "blank" field.

Demo Code:

#!/usr/bin/perl use strict; use warnings; my @lines = ("a b c\n", " a b c\n"); foreach my $line (@lines) { my @array = split ' ',$line; print join ("|",@array), "\n"; } foreach my $line (@lines) { my @array = split /\s+/,$line; print join ("|",@array), "\n"; } __END__ #using split on ' ' a|b|c a|b|c # using split on /\s+/ (the default) a|b|c |a|b|c

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found