Hello Folks
I am working on extracting lines
of data in a text file based
on the string it contains.
This is the text file called info.txt:
701,apple,red,r ound,cider
214,car,pink,sp orty,cute,
229,knife,silve r,sharp,cut
The program i am working on asks for input from a user .
Thus if the user enters 214 the program
then matches the number to a particular line ( in the text file ) starting with that number (in this case the second line)
and prints out everything on only that particular line.
And this is the program
i have so far
print "type your number";
$num=<STDIN>;
chomp($num);
open (DAT, 'info.txt') || die "file not opened";
@line=<DAT>;
foreach (@line)
{
chomp;
if ($_=~ m/^$num/)
{
print "$_\n";
}
}
close (DAT);
I am learning perl as my first programming language and
any help will be appreciated
Thanks in advance.
3 1754 KevinADC 4,059
Recognized Expert Specialist
Do you need to turn your homework in tomorrow?
See the other forum where you have ths posted.
numberwhun 3,509
Recognized Expert Moderator Specialist
Hello Folks
I am working on extracting lines
of data in a text file based
on the string it contains.
This is the text file called info.txt:
701,apple,red,r ound,cider
214,car,pink,sp orty,cute,
229,knife,silve r,sharp,cut
The program i am working on asks for input from a user .
Thus if the user enters 214 the program
then matches the number to a particular line ( in the text file ) starting with that number (in this case the second line)
and prints out everything on only that particular line.
And this is the program
i have so far
print "type your number";
$num=<STDIN>;
chomp($num);
open (DAT, 'info.txt') || die "file not opened";
@line=<DAT>;
foreach (@line)
{
chomp;
if ($_=~ m/^$num/)
{
print "$_\n";
}
}
close (DAT);
I am learning perl as my first programming language and
any help will be appreciated
Thanks in advance.
I am not sure which other forum you posted this to (thanks for the heads up Kevin), but hopefully they guided you correctly.
What I see is that you are trying to use a variable in a Regular Expression and that is not possible. Variable Interpolation does not happen in a regex match. You have to produce a regex that would match the data you are trying to identify. The regex isn't that bad and shouldn't be to hard to figure out. Why not try and write an appropriate regex and if you are still having issues, repost here for more assistance. Be sure not to keep cross posting between different forums as a number of us do lurk/post to other forums as well ( as you found out).
Regards,
Jeff
KevinADC 4,059
Recognized Expert Specialist
he/she is getting help on the other forum.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: lecichy |
last post by:
Hello
Heres the situation:
I got a file with lines like:
name:second_name:somenumber:otherinfo
etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
|
by: v4vijayakumar |
last post by:
is there any standard way to extract output from system(3) function?
TIA.
|
by: georges the man |
last post by:
The purpose:
• Sorting and Searching
• Numerical Analysis
Design Specification
You are to write a program called “StockAnalyser”. Your program will read a text file that
contains historical price of a stock. The program will allow users to query the stock price of a
particular date and output its statistics (mean price, median price and standard deviation) for any
specified period. Your program must be menu driven and works as follows.
|
by: Jack Wu |
last post by:
Hi
I've spent a good majority of my day trying to figure out how to have
PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I
have not gotten anywhere. Could somebody please help me...
I've scoured all the documentation, google, and mailing lists to no
avail. I believe the problem may lay in a jpeglib problem with OSX
10.3.9, or a python paths problem.
|
by: raypjr |
last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the particular elements in the List Box. Any suggestions on how to best write this?
' I create a Structure with members
' place, title, author, publisher, price, description, lastweek, weeksonlist
Structure bestseller
Public place...
| |
by: raypjr |
last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the particular elements in the List Box. Any suggestions on how to best write this?
' I create a Structure with members
' place, title, author, publisher, price, description, lastweek, weeksonlist
Structure bestseller
Public place As Integer...
|
by: Paulers |
last post by:
Hello,
I have a log file that contains many multi-line messages. What is the
best approach to take for extracting data out of each message and
populating object properties to be stored in an ArrayList? I have tried
looping through the logfile using regex, if statements and flags to
find the start and end of each message but I do not see a good time in
this process to create a new instance of my Message object. While
messing around with...
|
by: avik1612 |
last post by:
hi,
I have created a script in perl to extract n number of zip files to a particular destination after creating the folder of the zip file.
It is extracting . But when the zip file has subdirectories or folders inside it it ignores that and eztracts the whole contents without making folders
what shout it do I am using activeperl in Windows XP
i have pasted the code
|
by: Ant |
last post by:
Hi all,
My kids have a bunch of games that have to be run from CD (on Windows
XP). Now they're not very careful with them, and so I have a plan.
I've downloaded a utility (Daemon Tools) which allows me to mount and
unmount virtual CD drives onto an ISO image. This works well, but I
want to wrap this in a Python GUI which will present them with a list
of games in a particular directory.
Essentially the steps are this:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |