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: Beginner:
How do I do this...

 



TotalNoob
Novice

Jul 2, 2009, 5:07 AM

Post #1 of 29 (4707 views)
How do I do this... Can't Post

Hi

I have no idea what I'm doing so I need help. Pirate

I need the script or what it's called, to read only .jp and not read .jpg

So I write \.jp but what do I do in the end to make it stop?

Also is there a list of all those irregular ways to type dots and backslash and so forth? Do they have a name?

Help is much appreciated! Sorry for my ignorance. Crazy


questionnaire
New User

Jul 2, 2009, 8:46 AM

Post #2 of 29 (4698 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

I'm pretty sure what you're looking for are called regular expressions. A quick google search should turn up what you're looking for. I don't understand your initial question though.


TotalNoob
Novice

Jul 2, 2009, 9:30 AM

Post #3 of 29 (4695 views)
Re: [questionnaire] How do I do this... [In reply to] Can't Post


In Reply To
I'm pretty sure what you're looking for are called regular expressions. A quick google search should turn up what you're looking for.



Exactly! Thanks :)




In Reply To
I don't understand your initial question though.



Ok I'll try elaborate. I have a text with something like this:

|\.at|\.cx|\.io|\.jp|\.nu|\.st|\.tk|\.ua

I want the file to read .jp but it also read .jpg

So how do I make it only read .jp and only that not ad the g or any other letter after...?

Thanks again!


ichi
User

Jul 2, 2009, 9:58 AM

Post #4 of 29 (4694 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post


In Reply To

In Reply To
So how do I make it only read .jp and only that not ad the g or any other letter after...?
Thanks again!

in this simple case, you don't need regex. Normal string checking will do
pseudocode:

Code
if (mystring == "jp" ){ 
........
}

use the equality operator.


KevinR
Veteran


Jul 2, 2009, 10:49 AM

Post #5 of 29 (4687 views)
Re: [ichi] How do I do this... [In reply to] Can't Post


In Reply To

In Reply To

In Reply To
So how do I make it only read .jp and only that not ad the g or any other letter after...?
Thanks again!

in this simple case, you don't need regex. Normal string checking will do
pseudocode:

Code
if (mystring == "jp" ){ 
........
}

use the equality operator.


== is for numbers
"eq" is for strings
-------------------------------------------------


KevinR
Veteran


Jul 2, 2009, 10:52 AM

Post #6 of 29 (4686 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post


In Reply To

In Reply To
I'm pretty sure what you're looking for are called regular expressions. A quick google search should turn up what you're looking for.



Exactly! Thanks :)




In Reply To
I don't understand your initial question though.



Ok I'll try elaborate. I have a text with something like this:

|\.at|\.cx|\.io|\.jp|\.nu|\.st|\.tk|\.ua

I want the file to read .jp but it also read .jpg

So how do I make it only read .jp and only that not ad the g or any other letter after...?

Thanks again!


Assuming that the file extension is at the end of your string you want to add the end of string anchor ($) to your regexp:


Code
/\.(at|cx|io|jp|nu|st|tk|ua)$/


Note how I have used the dot once and created a group that you can check after the dot.
-------------------------------------------------


(This post was edited by KevinR on Jul 2, 2009, 10:52 AM)


TotalNoob
Novice

Jul 2, 2009, 10:59 AM

Post #7 of 29 (4683 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To

In Reply To

In Reply To
in this simple case, you don't need regex. Normal string checking will do
pseudocode:

Code
 if (mystring == "jp" ){  
........
}

use the equality operator.


== is for numbers
"eq" is for strings

Ok guys tnx but I have no idea what either of that is.... sorry Unsure


TotalNoob
Novice

Jul 2, 2009, 11:14 AM

Post #8 of 29 (4681 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To
Assuming that the file extension is at the end of your string you want to add the end of string anchor ($) to your regexp:


Code
 /\.(at|cx|io|jp|nu|st|tk|ua)$/


Note how I have used the dot once and created a group that you can check after the dot.



Ok maybe I used a too liniar example or maybe it will work but I have to ask... Will it work the same way if my text looks like this:

.at|cone|\.cx|buster|\.do|\.ef|club|rance|\.io|intership|\.li|jewelry|\.ni|\.nu|lions|movie|soho|\.sa|\.tk|teak|\.ua

Can I change it to something like:

buster|club|cone|/\.(at|cx|do|ef|io|li|ni|nu|sa|tk|ua)$/|rance|intership|jewelry|lions|movie|soho|teak|

where I move all those .xx's together.

Will the $ stop it from adding letters? If so will it work on other words too? Like if I want it to look at bust but not at buster, I'll add the $ after and have bust$ ? Did I get it right?


KevinR
Veteran


Jul 2, 2009, 11:18 AM

Post #9 of 29 (4679 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

At this point I am confused as to exactly what you are needing to do. Post some samples of the data you are working with and explain what you are trying to do with that data.
-------------------------------------------------


TotalNoob
Novice

Jul 2, 2009, 11:32 AM

Post #10 of 29 (4678 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To
At this point I am confused as to exactly what you are needing to do. Post some samples of the data you are working with and explain what you are trying to do with that data.



Well that is what I'm working with. It's a notepad file that contains all the commands separated by a square. One string is a lot of words like the one I created above, just 100s more of them separated by |

I'm trying to get it to read some words exactly as I type them.

I'm sorry for not being able to explain better. I'm really thankful that you are trying to help. =)


KevinR
Veteran


Jul 2, 2009, 12:03 PM

Post #11 of 29 (4675 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

Sorry, I still do not understand. Do you have some perl code you have already written? Maybe it will help understand.
-------------------------------------------------


TotalNoob
Novice

Jul 2, 2009, 12:05 PM

Post #12 of 29 (4673 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To
Sorry, I still do not understand. Do you have some perl code you have already written? Maybe it will help understand.



No I haven't, I'm only trying to change the variables/rules.


KevinR
Veteran


Jul 2, 2009, 1:00 PM

Post #13 of 29 (4671 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

OK...you have a file I guess with a line or lines like you posted, which appears to be a pipe delimted list of file extensions (that they are file extension is not really important).

You want the user to input something, I assume .jp or maybe just jp, and you want to see if that exact match is found in the line(s)?
-------------------------------------------------


ichi
User

Jul 2, 2009, 4:42 PM

Post #14 of 29 (4665 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To

In Reply To

In Reply To

In Reply To
So how do I make it only read .jp and only that not ad the g or any other letter after...?
Thanks again!

in this simple case, you don't need regex. Normal string checking will do
pseudocode:

Code
if (mystring == "jp" ){ 
........
}

use the equality operator.


== is for numbers
"eq" is for strings

that's one of the things i hate about Perl. Why have 2 syntax to check for equality. anyway, that said, its just a pseudocode for OP to explore, not a full solution.


(This post was edited by ichi on Jul 2, 2009, 4:43 PM)


vikas.deep
User

Jul 2, 2009, 11:29 PM

Post #15 of 29 (4658 views)
Re: [ichi] How do I do this... [In reply to] Can't Post


Code
that's one of the things i hate about Perl. Why have 2 syntax to check for equality. anyway,



Dear Ichi,

YOU can't have best of both the worlds or (if I am right) have a cake and eat it too. You hate to use two different syntaxs for equality but you forgot that perl has simplified variable declaration by letting you to use a scalar($) for whatever be the value. It is precisely for this reason that there are two different syntaxes for equality because how will perl know that that $something is a number or string. In C you have to predeclare int (something) so that the computer knows that this is a number and processes it accordingly. For that matter in C there are a lot of variable types which complicate matters In Perl you don't have to worry about the exact variable type but you have to pay a price later on.


Code
  that said, its just a pseudocode for OP to explore, not a full solution.



Haa! Haa! you find it difficult to accept your fault(even if it was a pseudocode). Same with me. May be because you take it (perl and your posts) very sincerely. Believe me after posting anything here I am also very worried that if I have said the right things and feel relieved only when I read thanks in reply to my posts and I hate to read that my code is not working or I am caught on wrong foot.


Code

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jul 2, 2009, 11:30 PM)


TotalNoob
Novice

Jul 3, 2009, 1:02 AM

Post #16 of 29 (4652 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To
OK...you have a file I guess with a line or lines like you posted, which appears to be a pipe delimted list of file extensions (that they are file extension is not really important).

You want the user to input something, I assume .jp or maybe just jp, and you want to see if that exact match is found in the line(s)?



Yes it's pipe delimited but they are both file extentions and words and numbers, all in one big pile. The program itself works, and I don't mess with that or know anything about it. Right now the program matches everything starting with the words entered in the pipe line. I want it to only recognize the exact things entered in that line, for instance only .jp and not .jpes or .jparg or anything else.

I may have found a solution but could you verify if it is correct? Using the example from my other post would adding \b in the end work you think? So it would look like this:

.at\b|cone\b|\.cx\b|buster\b|\.do\b|\.ef\b|club\b|rance\b|\.io\b|intership\b|\.li\b|jewelry\b|\.ni\b|\.nu\b|lions\b|movie\b|soho\b|\.sa\b|\.tk\b|teak\b|\.ua\b

or should it be:

buster\b|club\b|cone\b|/\.(at|cx|do|ef|io|li|ni|nu|sa|tk|ua)\b/|rance\b|intership\b|jewelry\b|lions\b|movie\b|soho\b|teak\b|

Or is there a better way? \b seems to work fine but it would be great to know if I mess up something or not. Thanks again! :)


KevinR
Veteran


Jul 3, 2009, 1:10 AM

Post #17 of 29 (4651 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

TotalNoob,

You can use the \b anchor. If I had a clearer understanding of what you are doing I could probably be more helpful. But I don't.

Is this supposed to be a regexp?


Code
.at\b|cone\b|\.cx\b|buster\b|\.do\b|\.ef\b|club\b|rance\b|\.io\b|intership\b|\.li\b|jewelry\b|\.ni\b|\.nu\b|lions\b|movie\b|soho\b|\.sa\b|\.tk\b|teak\b|\.ua\b


It sort of looks like it is but you post it totally out of context so I can't tell how you are using it. I asked you earlier if you had written any perl code and you said no. But now you said the above works so I assume you have written some perl code now? If so it would really help to see it.
-------------------------------------------------


TotalNoob
Novice

Jul 3, 2009, 2:34 AM

Post #18 of 29 (4647 views)
Re: [KevinR] How do I do this... [In reply to] Can't Post


In Reply To
TotalNoob,

You can use the \b anchor. If I had a clearer understanding of what you are doing I could probably be more helpful. But I don't.

Is this supposed to be a regexp?

I'm sorry I'm not able to explain better. English is not my first language and I don't know a lot about perl. By regexp do you mean Regular Expression? Then yes I think that is what I am supposed to use. Is it correct that \b stops the text?


In Reply To
It sort of looks like it is but you post it totally out of context so I can't tell how you are using it. I asked you earlier if you had written any perl code and you said no. But now you said the above works so I assume you have written some perl code now? If so it would really help to see it.



No I never wrote any perl code, and I'm not sure how I would see it and send it here. I'm only tweaking one parameter/component/line/variable that the program looks at. This is in a notepad file and looks like the example above with pipes, just a lot longer. So I don't have any perl code from the program itself only a reference file with pipes in the parameter, but I know it has to be written in perl to work, that's why I ask if there is a perl code that can stop the text.

I hope that helps some more. Thanks again


KevinR
Veteran


Jul 3, 2009, 8:06 AM

Post #19 of 29 (4644 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

Sorry mate but I am at the point that I can't help anymore. I have no clue what you are actually doing, and if what you have works, then I guess you can use it for whatever it is you are doing.

Regards,
Kevin
-------------------------------------------------


TotalNoob
Novice

Jul 4, 2009, 4:47 PM

Post #20 of 29 (4632 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post

Can someone else explain to me the exact meaning of \b in a string, please?

Also if it's possible for someone to tell me the difference between \b and \B ?



And please make the explanations as simple as you possibly can, thanks.


ichi
User

Jul 4, 2009, 8:01 PM

Post #21 of 29 (4628 views)
Re: [vikas.deep] How do I do this... [In reply to] Can't Post


In Reply To
YOU can't have best of both the worlds or (if I am right) have a cake and eat it too. You hate to use two different syntaxs for equality but you forgot that perl has simplified variable declaration by letting you to use a scalar($) for whatever be the value. It is precisely for this reason that there are two different syntaxes for equality because how will perl know that that $something is a number or string.

yes i can. i can switch to a language that doesn't have that kind of distinction. eg Python

Code
if input == 3:


Code
if input == "astring":

here, i can use == for both. Now i can have my cake and eat it.
As for the rest of your comments, sorry but i won't bother understanding and replying them since they are OT.


(This post was edited by ichi on Jul 4, 2009, 8:03 PM)


vikas.deep
User

Jul 4, 2009, 9:10 PM

Post #22 of 29 (4621 views)
Re: [ichi] How do I do this... [In reply to] Can't Post

Ichi,

I am ignorant about Python If you can do it in Python good but I still

believe that the reason for not been able to that in Perl is as said before

(to the best of my understanding), please correct me if you think

otherwise.

Further I said (it seems) you find it difficult to accept your mistakes

because you said you hate something (using two different syntaxes for

strings and numbers) in Perl and in the same breath you were offering

your explanation that it was a pseudo code. Either of the two can be

wrong at one time either you or Perl not both of these; mutually exclusive

events you see.

If perl is wrong no need of saying that "it was a pseudocode " if you were

wrong say "yes i was wrong instead of saying that I hate Perl". In a perl

forum largely you should expect people ignorant of other languages.

Next true even though (I understand) there was nothing perlish

in rest of my post but please explain "OT", so that next time I can be

more careful while replying or submitting any comments to your post.

(No offence was intended and no body should have been offended by

that innocuous non-perlish stuff in my post).



Had I seen "Sorry I was wrong in my pseudo-code" this line in your post

I would hae never said or posted another word here and I will not post

anything new here I think I have made my point very clear to the best

of my ability.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jul 4, 2009, 9:32 PM

Post #23 of 29 (4620 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post


Code
Perl defines the following zero-width assertions:   
\b Match a word boundary
\B Match except at a word boundary
\A Match only at beginning of string
\Z Match only at end of string, or before newline at
the end
\z Match only at end of string
\G Match only at pos() (e.g. at the end-of-match position of prior m//g)

A word boundary (\b ) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters
off the beginning and end of the string as matching a \W . (Within character classes \b represents backspace rather than a word boundary, just as it normally does in any double-quoted string.)

For details please see http://perldoc.perl.org/perlre.html#Regular-Expressions

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


ichi
User

Jul 4, 2009, 10:02 PM

Post #24 of 29 (4617 views)
Re: [vikas.deep] How do I do this... [In reply to] Can't Post


In Reply To
Further I said (it seems) you find it difficult to accept your mistakes
because you said you hate something (using two different syntaxes for
strings and numbers) in Perl and in the same breath you were offering
your explanation that it was a pseudo code. Either of the two can be
wrong at one time either you or Perl not both of these; mutually exclusive
events you see.


In Reply To
If perl is wrong no need of saying that "it was a pseudocode " if you were
wrong say "yes i was wrong instead of saying that I hate Perl".

do you have a problem with people posting pseudocode as a possible solution? Further, i did not say i hate Perl totally that I discourage people to use it, have i ?


In Reply To
in rest of my post but please explain "OT", so that next time I can be

OT means out of topic.



In Reply To
Had I seen "Sorry I was wrong in my pseudo-code" this line in your post
I would hae never said or posted another word here and I will not post
anything new here I think I have made my point very clear to the best
of my ability.

a pseudocode is just an illustration of an algorithmic idea, independent of what the syntax might be. If i really want to post a full solution, do you think i would not know the difference between eq and == ?? (FYI, i have been programming Perl and Python since the 90's. ) I can quite confidently say, i know what's going on.


KevinR
Veteran


Jul 4, 2009, 11:24 PM

Post #25 of 29 (4613 views)
Re: [vikas.deep] How do I do this... [In reply to] Can't Post


In Reply To
perl has simplified variable declaration by letting you to use a scalar($) for whatever be the value. It is precisely for this reason that there are two different syntaxes for equality because how will perl know that that $something is a number or string.


That is the exact reason. Perl does not have separate data types for numbers of strings, so you have to use the correct comparison operator to get the comparison context correct. Perl is unique in this way, but it was made for parsing and generating reports, so its no surprise.
-------------------------------------------------


TotalNoob
Novice

Jul 5, 2009, 3:22 PM

Post #26 of 29 (1435 views)
Re: [vikas.deep] How do I do this... [In reply to] Can't Post


In Reply To

Code
Perl defines the following zero-width assertions:    
\b Match a word boundary
\B Match except at a word boundary
\A Match only at beginning of string
\Z Match only at end of string, or before newline at
the end
\z Match only at end of string
\G Match only at pos() (e.g. at the end-of-match position of prior m//g)

A word boundary (\b ) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters
off the beginning and end of the string as matching a \W . (Within character classes \b represents backspace rather than a word boundary, just as it normally does in any double-quoted string.)

For details please see http://perldoc.perl.org/perlre.html#Regular-Expressions

Quote

Thank you, but I already read this info on that (and many other) sites and I don't understand it. It is as far as I understand, written for people that already know something perl. I don't, and that information doesn't make it any clearer. That's why I thought someone here could explain it in simple words, but I was wrong. Sorry for have taken your time.


vikas.deep
User

Jul 5, 2009, 10:56 PM

Post #27 of 29 (1424 views)
Re: [TotalNoob] How do I do this... [In reply to] Can't Post


Quote
Thank you, but I already read this info on that (and many other) sites and I don't understand it. It is as far as I understand, written for people that already know something perl. I don't, and that information doesn't make it any clearer. That's why I thought someone here could explain it in simple words, but I was wrong. Sorry for have taken your time.


Dear Friend

perl documentation is different from "many other sites".

In case of any doubt what so ever. There are only two authentic sources that one is referred to One the "Camel book" (now in 3rd ed) and Second Perl documentation itself.

You like everybody else should have a personal copy of "Camel book" (I am having 2ed) but let me tell (forewarn) you that except for the first few chapters rest of it you may find tough to understand.

But again if you are genuinely interested in learning Perl you will have to go through the Camel book at some or the other point in your life / carrier, so why not start today.

Forums like this help but they cannot be a replacement for building a foundation; programming tips or sharing of knowledge can only be a supplement to your efforts.

You need "Conceptual clarity" and for that best source is the camel book (No Body can ever dispute this), please spend some time reading and (more importantly) understanding it.

As you might already be knowing there are no easy solutions to anything

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jul 5, 2009, 11:08 PM)


shawnhcorey
Enthusiast


Jul 6, 2009, 5:18 AM

Post #28 of 29 (1418 views)
Re: [vikas.deep] How do I do this... [In reply to] Can't Post


In Reply To
In case of any doubt what so ever. There are only two authentic sources that one is referred to One the "Camel book" (now in 3rd ed) and Second Perl documentation itself.


FYI, much of what is in the Camel book is already available to you. perldata is an almost word for word copy of chapter 2, and perlfunc is chapter 3.

To get a listing of Perl documentation, type in:

Code
perldoc perl


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


vikas.deep
User

Jul 7, 2009, 12:20 AM

Post #29 of 29 (1409 views)
Re: [shawnhcorey] How do I do this... [In reply to] Can't Post

shawnhcorey

Dear Sir

I am a bit old- fashioned and always prefer a textbook for learning, where in you can if you want underline important points and while reading jot down notes in your note-book.
Only after doing the above I switch on / boot my computer.

No doubt I will always prefer reading Camel book.



Quote
much of what is in the Camel book is already available to you. perldata is an almost word for word copy of chapter 2, and perlfunc is chapter 3.


This does not really means that Camel book is out, what about other chapters, I have not checked if other chapters are also word by word available in documentation.

A bit out of topic but even if that were true, can you dispute the feel of pen and paper. Remember only hard copy of any document is taken as an authentic version.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


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

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