Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Scalar function

by johngg (Abbot)
on Sep 07, 2012 at 22:45 UTC ( #992399=note: print w/replies, xml ) Need Help??


in reply to Scalar function

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992399]
help
Chatterbox?
[Lady_Aleena]: Your Mother, that's the command line grep, not perl's grep.
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-05-23 19:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?