Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Array construct and foreach loop

by perl_5eeker (Novice)
on Apr 04, 2019 at 15:54 UTC ( [id://1232155]=perlquestion: print w/replies, xml ) Need Help??

perl_5eeker has asked for the wisdom of the Perl Monks concerning the following question:

Hello experts I am trying to run foreach loop on each element of array which looks like below (from Dumper) $VAR1 = 'abc def xyz'; foreach considers the above as a single element, I have tried splitting the array but I am missing something simple here, your help is much appreciated.

Replies are listed 'Best First'.
Re: Array construct and foreach loop
by choroba (Cardinal) on Apr 04, 2019 at 15:58 UTC
    What you've shown is a string, i.e. a scalar, not an array. Using split can turn it into a list; you can populate an array with a list, or you can iterate over it directly:
    #! /usr/bin/perl use warnings; use strict; my $string = 'abc def xyz'; # my @array = split ' ', $string; for my $part (split ' ', $string) { print "<$part>\n"; }
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found