CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
terminate search pattern

 



mkeith123
Novice

Mar 17, 2008, 12:26 PM

Post #1 of 23 (14291 views)
terminate search pattern Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:40 AM)


KevinR
Veteran


Mar 17, 2008, 2:06 PM

Post #2 of 23 (14289 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

What is this supposed to be or do?

my %cid1_rate = {/ls=a\w ls=a\w qw//[],/n qw//[],/n};

that does not appear to be valid perl code but I can't guess what you are trying to do there so I am unsure what to suggest. The proper way to declare a hash:

my %hash = (key => "value", key => "value");

or any function that returns a hash has to return it in that fashion. Is this school/class/course work?
-------------------------------------------------


mkeith123
Novice

Mar 19, 2008, 12:47 PM

Post #3 of 23 (14270 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:43 AM)


mkeith123
Novice

Mar 19, 2008, 1:04 PM

Post #4 of 23 (14267 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:55 AM)


KevinR
Veteran


Mar 19, 2008, 1:09 PM

Post #5 of 23 (14266 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

Mike,

The jpg image you posted is too blurry to read.

The perl code you have written is completely wrong unless you are using a version of perl I am totally unfamiliar with.

To read a file you must open it:


Code
open (IN, 'mv_0000001.txt') or die "$!"; 
while(<IN>){
here you read/parse the lines of the file
}
close IN


You must be in the directory the file is in or the file must be in the same folder as the script to open and read it with just using the filename. Or just use the full path to the file, if you know it, in the open() function. This line is just wrong for many reasons:


Code
my %cid1_rate = {/ls=a\w ls=a\w qw//[],/n qw//[],/n};


It is not going to do anything except throw an error. Your explanation makes no sense, maybe because the image is too blurry to read. Post the real lines of whatever it is you are trying to parse and it might make sense.
-------------------------------------------------


mkeith123
Novice

Mar 20, 2008, 12:01 PM

Post #6 of 23 (14258 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:43 AM)


KevinR
Veteran


Mar 20, 2008, 1:18 PM

Post #7 of 23 (14255 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

Mike, either I am confused or you are. Are you using Perl or something else? This line will do nothing:

my %cid1_rate = {/ls=a\w ls=a\w qw//[],/n qw//[],/n};

At least not in the Perl I know. Where did you get the idea that the above line will do anything? Everything you posted is totally wrong, that I am aware of. I think this is what you are trying to do:


Code
use strict; 
use warnings;
my %cid1_rate = (); #declare the hash
open (IN, 'mv_0000001.txt') or die "$!";
while(<IN>){
next if /^\s*1:/; #skip the line that starts with 1
my ($key,$val) = (split ',') [0,1];
$cid1_rate{$key} = $val;
}
close (IN);
foreach my $key (keys %cid1_rate) {
print "key = $key, value = $cid1_rate{$key}\n";
}


That will not print the key/value pair in the same order they were in the file because hashes have no guaranteed order. That could be fixed though by using an array.
-------------------------------------------------


mkeith123
Novice

Mar 20, 2008, 3:02 PM

Post #8 of 23 (14250 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

Kevin

I am trying to self teach Perl programing, and I have only been at it for a few days, hence the total lack of competance. Sorry.

It appears what you wrote works, except that it runs in a command prompt window which closes as soon as its done, so I can't read the results.


KevinR
Veteran


Mar 20, 2008, 4:27 PM

Post #9 of 23 (14248 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

The behavior of the window opening and closing is what you would expect if you are clicking on the perl file to run it. Instead, open a DOS window and at the C prompt type:

perl scriptname.pl

that way the DOS window stays open and you can read the results from the perl script. If perl is not in the command path use the full path to perl, something like:

c:\>perl\bin\perl.exe scriptname.pl
-------------------------------------------------


mkeith123
Novice

Mar 20, 2008, 6:15 PM

Post #10 of 23 (14246 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:56 AM)


KevinR
Veteran


Mar 20, 2008, 10:22 PM

Post #11 of 23 (14241 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post


In Reply To
Kevin

It works! The only thing is, there are only about half the key value pairs printing out. I found one of the key value pairs, and it is correct, but still I could not find many others. It seems what is there is correct, just not complete.



Mike


Mike, it is correct for the sample data you posted:

1:
124578,2,2003-03-04
26673,4,2005-12-11


Thats all I can go by. If the real data is different you have to adjust the code.
-------------------------------------------------


mkeith123
Novice

Mar 21, 2008, 11:14 AM

Post #12 of 23 (14230 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:54 AM)


KevinR
Veteran


Mar 21, 2008, 11:37 AM

Post #13 of 23 (14229 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

If Notepad does not display the file correctly it was maybe written on a different operating system than the one you use to view it with or was written using a word processor and uses line endings Notepad does not recognize (as well as other data Notepad may not understand).

For the sample lines you posted my code splits on the comma but only returns the first two fields: [0,1] and assigns them to $key and $val respectively:


Code
     my ($key,$val) = (split ',') [0,1]; <--- an array slice 
$cid1_rate{$key} = $val;


It is similar to writing it like this:


Code
     my ($key,$val,undef) = split ','; 
$cid1_rate{$key} = $val;


where the third field returned from the split function is undefined (undef) essentially deleting it right after creating it.

If the "key" is repeated in the file, for example:

124578,2,2003-03-04 #2nd line

if 124578 is repeated again the previous key/value pair is overwritten with the new key/value pair because hash keys are unique, there can never be two identical hash keys in the same hash. If you have a hash key that can have more than one value you would use a different data structure: a hash of arrays probably. But they might be getting too far ahead in your perl education. The code you posted previoulsy is so entirely wrong in concept (and syntax) that I think you may have skipped too much of the basic perl concepts, you might want to review your progress.

That is just a suggestion based only on what I can see, as far as I know you are another Einstein and could learn perl in a couple of days if you wanted to. It took me a few years to get to what I would call competent with perl, but I am also largely self taught and was in no hurry to learn perl, I picked it up as I went along on a need-to-know basis and have large gaps in my perl knowledge still left to be filled in.
-------------------------------------------------


mkeith123
Novice

Mar 21, 2008, 7:21 PM

Post #14 of 23 (14228 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

Kevin

Well now you know why my perl is so ugly, but at least 'i am trying. I was trying to do everthing in one line, which obviously doesn't work. I needed to declare the hash etc.

OK, so when the split sees the first comma, it assigns the number to the left as the key, and the number to the right as its value? What about the second comma in each line?

Mike


KevinR
Veteran


Mar 21, 2008, 11:00 PM

Post #15 of 23 (14224 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post


In Reply To
Kevin

Well now you know why my perl is so ugly, but at least 'i am trying. I was trying to do everthing in one line, which obviously doesn't work. I needed to declare the hash etc.

OK, so when the split sees the first comma, it assigns the number to the left as the key, and the number to the right as its value? What about the second comma in each line?

Mike


I give you plenty of credit for trying. To me that says a lot. I much prefer to help people that are trying and seem interested in learning and not in just getting answers.

As far as split() goes. The way I wrote it, using an array slice, Perl only returns the first two fields created by splitting on the pattern (the comma in this case). The second comma and the rest of the line is just ignored. If you want all the fields (or columns as they are often called) returned you would split to an array or a list that has scalars for all the fields.
-------------------------------------------------


mkeith123
Novice

Mar 23, 2008, 9:06 AM

Post #16 of 23 (14200 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:57 AM)


KevinR
Veteran


Mar 23, 2008, 10:51 PM

Post #17 of 23 (14186 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

The file you posted returns 548 key/value pairs when I run it through this code:


Code
my %h; 
<DATA>; # skip first line
while(<DATA>){
my ($k,$v) = (split ',')[0,1];
$h{$k}=$v;
}

print scalar keys %h; #prints the number of keys - 548


I don't know why you would get less.
-------------------------------------------------


mkeith123
Novice

Mar 24, 2008, 7:46 AM

Post #18 of 23 (14180 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

Kevin

Mine comes up in a seperate commmand prompt window, then at the end it says press any key to continue. When I do that the window closes. I am running vista on my laptop. There are about 250 pairs. Must be something on my end. I try running it on my pc with xp.

What do you think about comparison code?

Mike


KevinR
Veteran


Mar 24, 2008, 9:39 AM

Post #19 of 23 (14176 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

Mike,

I read the part where you said you got about 250 keys, the rest of your post I didn't read. Now I scanned through it and it is not something I am interested in helping with.
-------------------------------------------------


mkeith123
Novice

Mar 24, 2008, 10:02 AM

Post #20 of 23 (14174 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 9:59 AM)


KevinR
Veteran


Mar 24, 2008, 10:34 AM

Post #21 of 23 (14173 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

If there really is a million dollar prize, you should hire a really good programmer to help you. I'm not that person. I am a self taught perl coder, I would not make any claims to being a programmer. If you ever want to send me a donation, for whatever reason, send me a PM and I will let you know how.
-------------------------------------------------


KevinR
Veteran


Mar 24, 2008, 10:40 AM

Post #22 of 23 (14172 views)
Re: [mkeith123] terminate search pattern [In reply to] Can't Post

By the way, is this the NetFlix Million Dollar "predictions accuracy" contest?
-------------------------------------------------


mkeith123
Novice

Mar 25, 2008, 8:24 AM

Post #23 of 23 (14167 views)
Re: [KevinR] terminate search pattern [In reply to] Can't Post

 


(This post was edited by mkeith123 on Mar 28, 2008, 10:00 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives