Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Nested Loop Problems

by brx (Pilgrim)
on May 02, 2012 at 20:11 UTC ( [id://968529]=note: print w/replies, xml ) Need Help??


in reply to Nested Loop Problems

Take care to context (list or scalar) when you use split.

#!perl my @lines=<DATA>; for my $line (@lines) { chomp $line; my @arr = split/,/,$line; print "$arr[0]: $arr[1], $arr[2] and $arr[3]\n"; } __DATA__ name1,a1,b1,c1 name2,a2,b2,c2

Replies are listed 'Best First'.
Re^2: Nested Loop Problems
by cuautemoc (Initiate) on May 02, 2012 at 22:15 UTC

    Awesome, that worked. I'm still confused as to context; was I treating it as a scalar? I looked through help file for split, but I guess I don't understand the difference.

    Anyway, I've got it working. Thanks for the advice!

      About variables and context, see pelrdata

      $list=split ...: 'split' in scalar context because '$list' is a scalar value (split: In scalar context, returns the number of fields found.)

      @list=split ...: 'split' in list context and returns a list because '@list' expects a list value.

Log In?
Username:
Password:

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

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

    No recent polls found