Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Unscalar'ize a fake array

by Marshall (Canon)
on Sep 17, 2017 at 18:55 UTC ( [id://1199559]=note: print w/replies, xml ) Need Help??


in reply to Unscalar'ize a fake array

Another slight variation. Use foreach my $line (@array){...} if you have an array like this.

Update: I looked back at this thread and I admit to being flummoxed by what a "fake array" could be? Question: Do you have an array or not? That is a yes or no question. I'm not sure at all what a "fake" array could be? There could be a sequence of lines in a file. There could be multiple lines contained in a scalar text variable. I wouldn't characterize either of those situations as a "fake" array. I've worked with a lot of students in various programming languages, but I've never heard anybody refer to a "fake" array. You either have an array or you don't. If it is not an array, then there is a better more precise CS word for what this is.

#!/usr/bin/perl use strict; use warnings; my $total = 0; my $num = 0; while (my $line = <DATA>) { my @tokens = split ' ', $line; # split removes line ending next unless @tokens == 3; # ignore blanks and header line $total += $tokens[-1]; # add last column $num++; } print "total = $total Average=",$total/$num,"\n"; # prints: total = 127297 Average=31824.25 __DATA__ CHECKPOINT 2017-08-01 20MICRONS 37744 2016-08-01 20MICRONS 25966 2016-04-20 20MICRONS 30807 2016-04-01 20MICRONS 32780

Replies are listed 'Best First'.
Re^2: Unscalar'ize a fake array
by Gtforce (Sexton) on Sep 22, 2017 at 01:53 UTC

    Marshall, thanks for responding. I simply printed and eye-balled the array. Looking at it, I expected to be able to split by \t or whitespace, and it didn't split the array the way I hoped to - hence the frustration and reference.

      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://1199559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found