Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-25-2006, 01:59 PM   #1 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Ongoing perl help

Hi guys, got a project due in soon so may continually ask little questions, lets hope not.

Current problem;
I am comparing to strings
if ($string1 eq $string2)
but i want it so its less rigid. For instance, there would be a match if string1 was adacatgas and string2 was cat, so it would match if string2 existed anywhere within string1, is there a perl function or would i have to write one?

Thanks in advance

Last edited by pungi; 02-25-2006 at 03:40 PM.
pungi is offline   Reply With Quote
Old 02-25-2006, 10:25 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
short answer
Code:
$string1 = "adacatgas";
$string2 = "cat";
print "matched\n" if ($string1 =~ m/$string2/);
long answer: http://www.hmug.org/man/1/perlretut.php
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-25-2006, 10:36 PM   #3 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Thanks for the help
pungi is offline   Reply With Quote
Old 03-03-2006, 03:33 AM   #4 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Hi, dont worry with this query too much because its just for curiousity and i found it difficult to explain my problem.
Project deadline is over. It was based around using perl with flat-file databases, my job was to do searches particularily between 2 files. I only managed to get part of my searches working but too late to worry now. I had troubles with sorting the database, perhaps because of the methods i was using. I strictly read from files, store their info in arrays and did the searches based on that. Im thinking now that it wasnt the best way to go about it, since an array will only store one column of information.
My search was done by finding a match between a column in one file, whilst using a counter ($i++ each time i went through the loop), and using that variable value to print out the related data to the match by going through separate arrays, each array containing a column of information.
For example, where i found a match of rooms, i would grab that $i value, and extract the data like so:
# predefined arrays
@roomipark;
@roomicapacity;
@roominame;
@roomilab;

extracting the data with filled in arrays:
print"$roomipark[$i]";
print"$roomicapacity[$i]";
print"$roominame[$i]";
print"$roomilab[$i]";

So, how would i sort the data if i had a list of $i values?
I am thinking its the way that im storing the data thats at fault.
Perhaps associative arrays is the answer to my riddle, but to my understanding, those arrays only relate to two pieces of information. I would rather have an explantion to the technique i must use but if code helps u explain it then feel free. This is purely for curiosity as i am now going to convert the whole thing into a relational database using PHP and MYSQL. Thanks for reading and any help given,
pungi
pungi is offline   Reply With Quote
Old 03-07-2006, 02:25 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I think the approach you take sort of deoends on the structure of your data. If your data looks like this
Code:
identifier1	id1field1	id1field2	id1field3
identifier2	id2field1	id2field2	id2field3
then you can use associative arrays that key off of the identifier, and store the fields as an array in the other side of the the hash pairs e.g.
Code:
# %database{identifier1} => ('id1field1, id1field3, id1field3,);
@fields = ($field1, $field2, $field3);
$database{$identifier} = \@fields;
That way the identifier is basically the primary key (which must be unique) and looking up things based on that key is easy. Querying based other pieces of data means iterating through hash though.
Code:
@results;
foreach $record (keys(%database)) {
    foreach $field (@$database{$record}) {
        if( $field eq $myquery) {
            push(@results, $record);
        }
    }
}
Does that help? If I've been unclear or you need anything else, just ask.
__________________
Stop intellectual property from infringing on me

Last edited by teknomage1; 03-07-2006 at 03:14 PM.
teknomage1 is offline   Reply With Quote
Old 03-07-2006, 09:38 PM   #6 (permalink)
pungi
Recruit
 
Join Date: Feb 2006
Posts: 17
pungi is on a distinguished road
Thanks for the help. Thats a clever way of going about it, i have a fair idea of how the code works but need to use it to be sure in understanding. Just need to dedicate time to it so wont ask for help again with perl for a few weeks maybe or never if all goes well. I was given the hint of structs which i might use instead to give me a wider knowledge, though from what i read just now, associative arrays does look easier. Thanks.
pungi is offline   Reply With Quote
Old 03-08-2006, 11:41 AM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Perl's a unique language in that in some fashoion or another it supports most every programming paradigm and no two perl coders seem to approach a problem in the same manner.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
My first Perl script. Admin All Other Coding Languages 2 05-19-2005 11:09 AM
how come i dont hear much about perl Apodysophilia All Other Coding Languages 2 12-16-2003 10:05 AM


All times are GMT -8. The time now is 01:59 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting