Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am complete new in perl. I wrote a program which opens two different files but it doesn't read the first element of array, rather it reads whole line. Anyway, Not to make you more confused, here is an example
what.txt has this
1010101
what1.txt has this
0010000
I need this
10001100
which is just a first element of first file is combined with first element of second file and second element of first file combined with second element of second file...and so on. Thanks for your help
Re: Multiple file reading
by btrott (Parson) on Jul 27, 2000 at 01:20 UTC
|
Are those 1s and 0s bits or are they just characters?
If the latter, you could just read one byte at a time
from each file, then write them out:
open FH1, "what.txt" or die $!;
open FH2, "what1.txt" or die $!;
my($buf1, $buf2, $total);
while (read(FH1, $buf1, 1) && read(FH2, $buf2, 1)) {
$total .= $buf1 . $buf2;
}
close FH1;
close FH2;
Of course, you'd get faster results by reading in 1000 bytes
or so at a time, then grabbing the first byte of each buffer.
That would cut down on disk operations. | [reply] [d/l] |
RE: Multiple file reading
by jlistf (Monk) on Jul 27, 2000 at 01:15 UTC
|
i don't know what you mean by combined either, but perhaps:
open FILE1, "what.txt" or die "can't open what.txt : $!\n";
open FILE2, "what1.txt" or die "can't open what1.txt : $!\n";
while ($first = <FILE1> and $second = <FILE2>)
{
#combine strings somehow
#push onto an array
}
| [reply] [d/l] |
Re: Multiple file reading
by ase (Monk) on Jul 27, 2000 at 14:34 UTC
|
@a = split //,'xx110';
@b = split //,'001xx';
print map{$a[$_].$b[$_]}(0..$#a);
Golf anyone? :-) (My score: 33 not counting splits)
-ase | [reply] [d/l] |
RE: Multiple file reading
by BigJoe (Curate) on Jul 27, 2000 at 01:20 UTC
|
Sounds like you want to do a binary combine. If this is the case try using "xor, or, and" operators. Like this my $a = 10101010
my $b = 10000000
my $XorComb = $a xor $b
my $orComb = $a or $b
my $AndComb = $a and $b
I have never done this with perl (only in Assembly & C) I think you may have to either declare the varibles as binary or make do the compares in binary.
--BigJoe
Update use the operators ar0n said "For binary operations use &, | and ^." instead."
| [reply] [d/l] |
|
The operators 'and', 'or', and 'xor' are logical operators.
They will not work in this context (at least not the way
you want them to).
They are equal to &&, || and (!$a != !$b), respectively.
For binary operations use &, | and ^.
-- ar0n
| [reply] |
Multiple file reading
by Anonymous Monk on Jul 27, 2000 at 02:02 UTC
|
Wow! that was very quick response. Sorry about the confusion.I moved to USA about a year ago. My English is not good! Anyway, This is a little modification on previous question. I have two different files, They have 1s, 0s,and xs. I want the new file to write the first element of file1 with first element of file2 , in order you know.
Let's see, what.txt has this, xx110 and what1.txt has this,
001xx. I want, x0x0111x0x. First element of file1 with first element of file2 and so forth.You see what I mean. May be I am still not clear huh? Thank you very much for your responses though. Bye. Have a good day. | [reply] |
|
I think this is what you want:
@a = split //, 'xx110';
@b = split //, '001xx';
splice @a, ++$i, 0, $_ or $i++ for(@b);
print @a, "\n";
Results:
x0x0111x0xYou just have to read in each line of your files,
then split the characters into an array. The splice line
is sort of tricky. It inserts each element of @b into
@a, at the 1st, 3rd, 5th ... position of @a. I hope this helps. There may be
another and better way to merge arrays, but I cant think of
one right now. | [reply] [d/l] [select] |
RE: Multiple file reading
by jjhorner (Hermit) on Jul 27, 2000 at 01:05 UTC
|
I'm anxious to help, but I have no clue what you want.
When you say "combined" what operation are you doing?
Give us some more information.
J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/
| [reply] |
Re: Multiple file reading
by mrt (Acolyte) on Jul 27, 2000 at 18:01 UTC
|
Really I can not follow what you want...to quote you
:here is an example what.txt has this 1010101 what1.txt has
this 0010000 I need this 10001100: Now It is difficult to
follow where has 10001100 come from?? it is not easy to make out
what combination you have chosen for that??? | [reply] |
OT Other words you could have used
by Fastolfe (Vicar) on Jul 27, 2000 at 23:22 UTC
|
Rather than 'combine', you might have used 'blend' or 'merge'. There's also 'concatenate', but that typically means to combine one after the other (in entirety).
Also, you probably meant 'byte' or 'character' instead of 'element'.
Both of these changes would have greatly enhanced the legibility of your question. :)
Good luck. | [reply] |
|
|