Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Scalar function

by gon770 (Novice)
on Sep 07, 2012 at 16:52 UTC ( [id://992345]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, PerlMonks.

I am having trouble with new lines (\n). It seems like everything within scalar will print new line. For instance,

$_ and $fa
$fb
$fc

But, I want it to be $_ $fa $fb $fc in same line.
Should I try different function? Or there is an optional augument for scalar function? Thank you

while(<$fi>){ chomp; print CSV $_, scalar <$fa>, scalar <$fb>, scalar <$fc>; }

Replies are listed 'Best First'.
Re: Scalar function
by MidLifeXis (Monsignor) on Sep 07, 2012 at 17:11 UTC

    If $fa, $fb, and $fc are file handles, then

    print CSV $_, scalar <$fa>, scalar <$fb>, scalar <$fc>;
    is doing the equivalent of
    my $line_a = <$fa>; my $line_b = <$fb>; my $line_c = <$fc>; print CSV $_, $line_a, $line_b, $line_c;
    Note that there is no chomp on the reads from the three files.

    --MidLifeXis

Re: Scalar function
by johngg (Canon) on Sep 07, 2012 at 22:45 UTC

    As MidLifeXis points out, you need to chomp your newlines.

    $ perl -E ' > open my $fa, q{<}, \ <<EOF1 or die $!; > file A line 1 > file A line 2 > EOF1 > > open my $fb, q{<}, \ <<EOF2 or die $!; > file B line 1 > file B line 2 > EOF2 > > say join q{,}, > map { chomp; $_ } scalar <$fa>, scalar <$fb>;' file A line 1,file B line 1 $

    I hope this is helpful.

    Update: A more complete example putting the $fa, $fb and $fc filehandles in an array and showing how you might cope if they contain fewer lines than $fi using eof.

    $ perl -Mstrict -Mwarnings -E ' > open my $fi, q{<}, \ <<EOFi or die $!; > File I Line 1 > File I Line 2 > File I line 3 > EOFi > > open my $fa, q{<}, \ <<EOFa or die $!; > File A Line 1 > File A Line 2 > EOFa > > open my $fb, q{<}, \ <<EOFb or die $!; > File B Line 1 > File B Line 2 > File B Line 3 > EOFb > > open my $fc, q{<}, \ <<EOFc or die $!; > File C Line 1 > File C Line 2 > File C Line 3 > File C Line 4 > EOFc > > my @slaveFHs = ( $fa, $fb, $fc ); > while ( <$fi> ) > { > chomp; > say join q{,}, $_, > map { chomp; $_ } > map { eof $_ ? q{--eof--} : scalar <$_> } > @slaveFHs; > }' File I Line 1,File A Line 1,File B Line 1,File C Line 1 File I Line 2,File A Line 2,File B Line 2,File C Line 2 File I line 3,--eof--,File B Line 3,File C Line 3

    Cheers,

    JohnGG

Re: Scalar function
by philiprbrenan (Monk) on Sep 07, 2012 at 16:57 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-16 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found