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

precedence: two functions on one line

by kingman (Scribe)
on Jul 27, 2001 at 18:29 UTC ( [id://100312]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to fill @files with:
#$files[0] = '/usr/local/bin/foobar/a.html' #$files[1] = '/usr/local/bin/foobar/b.html' #$files[2] = '/usr/local/bin/foobar/c.html' #$files[3] = '/usr/local/bin/foobar/d.html' #$files[4] = '/usr/local/bin/foobar/e.html' #$files[5] = '/usr/local/bin/foobar/f.html'
here's the code:
$tracker = <<EOQ; a.html<br> b.html<br> c.html<br> d.html<br> e.html<br> f.html<br> EOQ $path = "/usr/local/bin/foobar/";
Splitting works fine
@ttt = split /<br>\n/, $tracker; for (@ttt) { print "$_\n"; }

My Question:

Should I be trying to do this by splitting and joining at once or is it better to do it in two separate calls? I guess I need to parenthesize things?

p220 of the Camel book 2nd ed. has this line:

print join ':', split / */, 'hi there';

Can I do something similar?

This part doesn't work yet:
@files = join split /<br>\n/, $tracker, $path; foreach (@files) { print "$_\n" }

Thanks!

Edit ar0n 2001-07-27 -- fixed formatting

Replies are listed 'Best First'.
Re: precedence: two functions on one line
by bwana147 (Pilgrim) on Jul 27, 2001 at 18:38 UTC

    Parentheses are mostly harmless, so you should use them when in doubt, or even when not in doubt, just to make things plain and visible.

    Now, as for your problem:

    $path = "/usr/local/bin/foobar/"; @file = map { "$path/$_.html" } qw/ a b c d e f /;

    --bwana147

      I can't change the data as this is part of a larger application that expects $tracker to be a scalar. However, This variation on what you posted does exactly what I want, and on a single line too!
      @files = map { "$path$_" } split /<br>\n/, $tracker;
      Must go meditate on the mysteries of the map function. Thanks!
        map's great. Here's another example, to aid the old meditations:
        $tracker = join "", map {"$_.html<br>\n"} ('a'..'f');
        - although frankly it's more readable as a here doc!

        andy.

Re: precedence: two functions on one line
by earthboundmisfit (Chaplain) on Jul 27, 2001 at 18:38 UTC
    Why not simply declare $tracker in list context?
    @tracker = qw(a.html b.html c.html d.html e.html f.html);
    You can then use this array as a seed for opening and slurping each file to @files.
Re: precedence: two functions on one line
by iakobski (Pilgrim) on Jul 27, 2001 at 18:38 UTC
    You appear to have left out the first argument to join, it should be the character you want to put between each item you join. Use a null string if you just want to join everything together.

    -- iakobski

      The first argument to join is supposed to be
      split /<br>\n/, $tracker
      The second arg is supposed to be:
      $path
Re: precedence: two functions on one line
by runrig (Abbot) on Jul 27, 2001 at 18:55 UTC
    You forgot the ":" argument to join, and you would do well with a map:
    @files = join ":", map { "$path/$_" } split /<br>\n/, $tracker;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found