[SOLVED] Get strings distributed along up to 3 lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was able to see what was happening with the incorrect printing of column1. The correct while loop is as below.
Code:
while ( defined ( my $l = <F> ) ) {
while (length $l < $MINIMAL_RECORD_LENGTH) {
#$l .= $sep; --> This line is not needed
$l .= <F>;
}
process ($l);
}
Hello grail,
I was trying to do the same with ruby, but doesn't work in the way below:
Code:
IO.foreach(ARGV[0]){ |l|
line = l.unpack('H*')[0]
while line.length < MINIMAL_RECORD_LENGTH do
line = line + l.unpack('H*')[0]
else
#Original code
end
} if File.exists?(ARGV[0])
Where and how would be the correct way to insert the while loop in ruby code?
I finally was able to make the comparison in perl in the long way (if.. else) since I was not able to make it short way, since
I don't know how to assign the pattern to a variable. I did like below.
In ruby I was able to do it in short way like below:
Code:
z = "|" + [fruit.compact,rest.scan(/../).map{|z| z.hex}].join("|")
compare=/2,13,8147526905,0\|\|\|12,13,8147526905,1,1\|\|12,13,814752695(59|65|86),0\|\|1\|2\|1\|2\|1\|0\|1\|255\|255\|255\|2\|1\|2\|1/
z = ( z=~ compare ? z . "|A" : z . "|B" )
Thanks in advance.
the perl way is quite similar:
$z .= ( ($z =~ <your regexp>) ? "|A" : "|B" );
The other thing: in this line:
$z= join ("|", @result[ 3 .. $#result);
the closing ] is missing.
About the separator: I think it will be missing (the length of the line will not be the same, but I have not tested, I do not really know if it is required or not).
Thank you. I was able to store in variable the regex with "$var = qr//" and use the short form of "if".
Hello grail,
The way that worked the suggestion of pan64 in post # 112 is like below:
Code:
while ( defined ( my $l = <F> ) ) {
while (length $l < $MINIMAL_RECORD_LENGTH) {
#$l .= $sep; --> This line is not needed
$l .= <F>;
}
process ($l);
}
But I'm still don't know how to apply it to your ruby code, I've tried like below but is failing.
Code:
IO.foreach(ARGV[0]){ |l|
line = l.unpack('H*')[0]
while line.length < MINIMAL_RECORD_LENGTH do
line = line + l.unpack('H*')[0]
else
#Original code here
end
} if File.exists?(ARGV[0])
line = ""
IO.foreach(ARGV[0]){ |l|
line = line + l.unpack('H*')[0]
while line.length < MINIMAL_RECORD_LENGTH do
# go to next cycle, so do nothing here
else
#Original code here
line = "" # empty
end
} if File.exists?(ARGV[0])
As it is the foreach that feeds the next line / segment I would suggest using the next operator and storing short readings in a temp variable.
Something like:
Code:
IO.foreach(ARGV[0]){ |l|
line = l.unpack('H*')[0]
if line.length < MINIMAL_RECORD_LENGTH
tmp_l = line
next
end
line = tmp_l + line if ! tmp_l.nil?
You would also need to set it back to nil at the end of the loop
Maybe I did wrong something, but I was able only to get it to work in this way.
Code:
tmp_l = ''
IO.foreach(ARGV[0]){ |l|
line = l.unpack('H*')[0]
if line.length < MINIMAL_RECORD_LENGTH
tmp_l = line
next
end
line = tmp_l + line
#Original code
tmp_l = ''
} if File.exists?(ARGV[0])
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.