473,795 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grabbing Text from file and assigning them to variables

1 New Member
Perl Newbie here.

I want to open a file that I have setup as a configuration file. After opening the file I was to assign parts of the words to variables.

for example (configuration file config.conf):

Expand|Select|Wrap|Line Numbers
  1. cake=yellow,chocolate,red,white
  2. icing=butter,lemon,chocolate
  3.  
I want to open the above file, config.conf, and read it while assiging variables to the items after the equals sign.

Expand|Select|Wrap|Line Numbers
  1. my($cake,$icing);
  2.  
  3. $cake = undef;
  4. $icing = undef;
  5.  
  6. open(handle,"config.conf") || die "no file found\n";
  7. foreach $line (<handle>) {
  8.     my( $cake) = $line =~ /^cake=(.*)/;
  9.     my($icing) = $line =~ /^icing=(.*)/;
  10. }
  11. print $cake; #nothing prints
  12. print $icing; #nothing prints
  13. close(handle);
  14.  
  15. #next i would like to split the $cake items by using the split command
  16. #then I would split the $icing items
  17. #tricky part I think is trying to create a directory structure where each cake has
  18. # below it a sub directory for each icing
  19. #I plan on populating taste results under the sub directory
  20. #ex:
  21.  
  • /yellow
    • /butter
    • /lemon
    • /chocolate
  • /white
    • /butter
    • /lemon
    • /chocolate

$cake and $icing do not print.

If someone could help with my logic I would appreciate the hlep.
Aug 29 '07 #1
2 1662
KevinADC
4,059 Recognized Expert Specialist
you have a scoping problem:

my($cake,$icing );#<-- here you declared with "my"

$cake = undef;
$icing = undef;

open(FH,"config .conf") || die "no file found\n";
while (my $line = <FH>) {
my( $cake) = $line =~ /^cake=(.*)/;#<-- $cake declared again with "my"
my($icing) = $line =~ /^icing=(.*)/;#<-- $icing declared again with "my"
}

just remove the "my" from the variables in the loop and it will work but it will assign the last match in the file to those variables. If there is only one possible match than you will be OK. Note I changed "foreach" to "while" and "filehandle " to "FH". "foreach" will not process a file properly and filehandles should aways be uppercase if you use barewords like FH or FILEHANDLE. This avoids collisions with functions and other things that are barewords.
Aug 29 '07 #2
miller
1,089 Recognized Expert Top Contributor
Alternatively, you could use a config module like Config::Tiny.

Expand|Select|Wrap|Line Numbers
  1. use Config::Tiny;
  2.  
  3. # Open the config
  4. $config = Config::Tiny->read( 'config.conf' );
  5.  
  6. my $cake = $config->{_}{cake};
  7. my $icing = $config->{_}{icing};
  8.  
  9. print "$cake\n";
  10. print "$icing\n";
  11.  
- Miller
Aug 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1952
by: Dave | last post by:
I am trying to create a function in SQL 2000. Im rusty on function syntax and need to also assign some variables for use within this function. Example: create function blah (@param1 int) declare @var1 int, @var2 int --it doesnt like this method of assigning values to my
4
38519
by: Jean-Marie Delapierre | last post by:
Hi, I have two files in a directory. one called toto.txt with the following content : 1 2 3 4 5
16
2132
by: chris | last post by:
im new to javascript but slowly getting better what i want to do is have some text on the screen and when an event happens for example click a button the text would change to what i want. how would i do this ??
4
1394
by: darrel | last post by:
I have a contact form that a person submits to the server. In ASP, you'd make a page, post the form to another page, which would grab the values and do somethign with them. in ASP.NET, it appears that you grab the values directly from the form objects on the postback. Here's our problem:
1
4236
by: Kermit Piper | last post by:
Hello, OK, almost there. Here's what I have so far, which handles characters as they're typed in. Could someone please show me how I would loop through all the values that are entered if a block of text was pasted in, and then grab the ascii value(s) that are > 128?: <script type="text/javascript"> function toASCII(s) {
1
1879
by: Jerry John | last post by:
I am working in ASP.NET with C#. I have a text file which contains datas with delimiters. For example:- MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939| I also have an XML file created with predefined tags. Some of the tags contain child element. I need to pass the data from the text file i.e the value within the delimiters should be passed to the corresponding tags within the XML file. I have done this through hard code. But i...
4
3862
by: Darren Mar-Elia \(MVP\) | last post by:
So, I have my little .Net 2.0 C# utilities out in the wild and occasionally they have bugs(!). Often the bugs are easy to troubleshoot remotely but occasionally I would like to write detailed trace info to a simple text file that I could use to further track down the bugs. Additionally, somewhat related, it would be useful to be able to get the line number where an exception was thrown, which is slightly different than a debug log, but...
5
1923
by: joe_doufu | last post by:
Hi, old web developer, new to Ajax and XML here. I'm developing an application that grabs an XML file from a (PHP) web service. In fact, it's going to be an online game. When the user clicks a map location it accesses a service that provides information about that location in a form something like this: <maplocation locID="###" picturesrc="image.jpg"> <name>...</name> <breadcrumbs>...</breadcrumbs> <description>...</description>
1
2113
roswara
by: roswara | last post by:
Dear all, Currently, I am working on a project to make a web-based application using ASP 2.0 and C#. This application will ask user to input for an excel file which has graphs in it. Then the application will grab the graph as image file, and this image will be displayed as thumbnail in the page. Is it possible to accomplish this feature? If it is possible, would you please tell me how? And if it is not possible, why?
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.