Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Converting string to array?

Reply
Thread Tools

Converting string to array?

 
 
Samuel Hardman
Guest
Posts: n/a
 
      09-11-2006
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


 
Reply With Quote
 
 
 
 
Michael Wehner
Guest
Posts: n/a
 
      09-12-2006
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.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-12-2006
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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-12-2006
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


 
Reply With Quote
 
Sam Hardman
Guest
Posts: n/a
 
      09-12-2006
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" <(E-Mail Removed)> wrote 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
STL: Converting a string into a list of string blrmaani C++ 4 04-27-2004 09:38 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments