LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2013, 12:36 AM   #121
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled

Hello pan64,

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?

Thanks in advance

Last edited by Perseus; 11-10-2013 at 12:48 AM.
 
Old 11-10-2013, 11:50 AM   #122
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Perseus View Post
Hello pan64,

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.
Code:
$z= join ("|", @result[ 3 .. $#result);	

if($z=~/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\|11/)
{
	push @result, "A";
	}
else {
	push @result, "B";
}
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).

Last edited by pan64; 11-10-2013 at 11:52 AM.
 
Old 11-10-2013, 11:58 AM   #123
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello pan64,

Thank you for your answer. Yes, the "]" is missing, I added.

The issue I had is how to assign that long regex to a variable, similar to how I done in ruby with variable "compare".

If I can assign the regex to a variable, I think could be easiest to use the short way of "if".

Thanks again.
 
Old 11-10-2013, 11:53 PM   #124
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello pan64,

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])
Maybe you can help me with this.

Thanks one more time both.

Regards
 
Old 11-11-2013, 12:25 AM   #125
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would do something like this:
Code:
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])
 
Old 11-11-2013, 02:59 AM   #126
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 11-11-2013, 11:16 PM   #127
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello grail/pan64,

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])
Many thanks for your help again
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending matching strings to specific lines (sed/bash) suntzu Programming 18 09-08-2012 03:29 PM
[SOLVED] search for 2 different strings in 2 diffrent lines threezerous Linux - Newbie 8 07-30-2012 03:42 PM
truncate strings on many lines mufea Linux - Newbie 2 02-23-2012 06:29 AM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-06-2008 11:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration