473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting string to array?

Hello,

I am trying to write a perl script to parse a string into an array. The
string has the fields separated by tabs. So what I want to do is read each
field into a variable so I can process the data further.

what I have is something like this;

open($rpt, $file);
while (<rpt>)
{
$line = $_;

#now what I want is something like
$field_1 = $line[0];
$field_2 = $line[1];
# of course this doesn't work so How do I do it?

}

An example string in the file might be like this.

995253 \t 1234 \t "address" \t "Name"
Thanks in advance for the help,
Sam
Sep 11 '06 #1
4 12779
Samuel Hardman wrote:
Hello,

I am trying to write a perl script to parse a string into an array. The
string has the fields separated by tabs. So what I want to do is read each
field into a variable so I can process the data further.
I suggest a simple regex. If you don't know about regexs yet, you're
going to be missing out on much of Perl's usefulness. There are several
very good texts available, including "Mastering Regular Expressions" by
Jeffrey Friedl. And of course, Perl's own documentation is more than
adequate learning for a beginner.

A regex to process your data could be as simple as:

my ($field_1, $field_2) = $line_of_text =~ /^(\S+)\s+(\S+)/;

Which would stuff the first two fields into the declared variables. A
while() loop can also be useful to match an arbitrary number of fields,
and push them onto a stack.

And of course there's always CPAN.
Sep 12 '06 #2
Samuel Hardman wrote:
Hello,

I am trying to write a perl script to parse a string into an array.
The string has the fields separated by tabs. So what I want to do is
read each field into a variable so I can process the data further.
See split(). It does exactly what you are asking for.

BTW: this NG has been discontinued about a decade ago and replaced by the
the comp.lang.perl.* hirarchy. If you Usenet provider still carries it
instead of the 'new' hirarchy you may wonder what else he is missing.

jue
Sep 12 '06 #3
Michael Wehner wrote:
Samuel Hardman wrote:
>Hello,

I am trying to write a perl script to parse a string into an array. The
string has the fields separated by tabs. So what I want to do
is read each field into a variable so I can process the data further.

I suggest a simple regex.
Rather no. What is that rule of thumb again?
If you know what to capture then use a RE, if you know what to throw away
then use split().

jue
Sep 12 '06 #4
Thanks so much for the quick response from both you and Michael. I haven't
programmed for over 4 years so I'm having to re-teach myself so much. I was
only a moderately skill perl programmer to begin with. So much to learn
again. I'm one of those DOT com layoffs from that moved on to other things
in life. Selling Real Estate, so really doing nothing at all in computers.
However this report is something we do often and having worked in build
process and automation, I reconized that we could automate this report......
So guess who gets to do it! LOL
Thanks again,
Sam

"Jürgen Exner" <ju******@hotmail.comwrote in message
news:9wzNg.7572$xC3.1150@trnddc06...
Samuel Hardman wrote:
>Hello,

I am trying to write a perl script to parse a string into an array.
The string has the fields separated by tabs. So what I want to do is
read each field into a variable so I can process the data further.

See split(). It does exactly what you are asking for.

BTW: this NG has been discontinued about a decade ago and replaced by the
the comp.lang.perl.* hirarchy. If you Usenet provider still carries it
instead of the 'new' hirarchy you may wonder what else he is missing.

jue

Sep 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
2
by: XML newbie: Urgent pls help! | last post by:
Does anyone have a snippet of code that will convert a string to a long array? I've nearly smashed my head against the wall trying to figure this out. I'm Using vb.net 2005 Pls reply asap. I...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.