473,372 Members | 1,314 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,372 software developers and data experts.

Query about using split...URGENT

Hello All
I am a newbie to PERL language...If i have a file with data of form

abcd 4 {1,2,3} 3
lmn- 3 {12,18,19,22} 4

then i can read them as...
($list $listTotal $set $noElements) = split / /

But if i have a dataset of the form:

abcd 4 {1,2,3} 3
{1,2}
{3}
lmn- 3 {12,18,19,22} 4{12}
{18,19}
{19,22}


i.e. I have more than two kinds of delimiters, then how should i read it.

Please, help...I am unable to go ahead with my work because i am stuck here.
Any kind of input will be appreciated. Thanks a lot in advance....

~uc_sk
Jul 19 '05 #1
8 2079
uc_sk wrote:
If i have a file with data of form

abcd 4 {1,2,3} 3
lmn- 3 {12,18,19,22} 4

then i can read them as...
($list $listTotal $set $noElements) = split / /

But if i have a dataset of the form:

abcd 4 {1,2,3} 3
{1,2}
{3}


lmn- 3 {12,18,19,22} 4
{12}
{18,19}
{19,22}


i.e. I have more than two kinds of delimiters, then
how should i read it.


Is

split /\s+/

what you are after?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2
Hello Gunnar
I didnt get what do you mean by "what you are after?"
Basically i am trying read variables/fields by teh delimiters and
instead of using the whole expression, i just want to use may be 1 or
two fields....let say in my output file i want to print just the $list
and $set or anyother two variables.

Thanks
~uc_sk

Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<5L********************@newsb.telia.net>...
uc_sk wrote:
If i have a file with data of form

abcd 4 {1,2,3} 3
lmn- 3 {12,18,19,22} 4

then i can read them as...
($list $listTotal $set $noElements) = split / /

But if i have a dataset of the form:

abcd 4 {1,2,3} 3
{1,2}
{3}


lmn- 3 {12,18,19,22} 4
{12}
{18,19}
{19,22}


i.e. I have more than two kinds of delimiters, then
how should i read it.


Is

split /\s+/

what you are after?

Jul 19 '05 #3
[ Please do not top post! ]

uc_sk wrote:
Gunnar Hjalmarsson wrote:
uc_sk wrote:
If i have a file with data of form

abcd 4 {1,2,3} 3
lmn- 3 {12,18,19,22} 4

then i can read them as...
($list $listTotal $set $noElements) = split / /

But if i have a dataset of the form:

abcd 4 {1,2,3} 3
>{1,2}
>{3}

lmn- 3 {12,18,19,22} 4
>{12}
>{18,19}
>{19,22}

i.e. I have more than two kinds of delimiters, then
how should i read it.


Is

split /\s+/

what you are after?


I didnt get what do you mean by "what you are after?" Basically i
am trying read variables/fields by teh delimiters and instead of
using the whole expression, i just want to use may be 1 or two
fields....let say in my output file i want to print just the $list
and $set or anyother two variables.


I meant that I wasn't sure of what's the field data and which the
separators are. Accordingly my suggestion above was a guess.

Since I'm still not sure, this is another guess:

split /[\s>]+/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #4
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<cN********************@newsb.telia.net>...
[ Please do not top post! ]

uc_sk wrote:
Gunnar Hjalmarsson wrote:
uc_sk wrote:
If i have a file with data of form

abcd 4 {1,2,3} 3
lmn- 3 {12,18,19,22} 4

then i can read them as...
($list $listTotal $set $noElements) = split / /

But if i have a dataset of the form:

abcd 4 {1,2,3} 3
>{1,2}
>{3}

lmn- 3 {12,18,19,22} 4
>{12}
>{18,19}
>{19,22}

i.e. I have more than two kinds of delimiters, then
how should i read it.

Is

split /\s+/

what you are after?
I didnt get what do you mean by "what you are after?" Basically i
am trying read variables/fields by teh delimiters and instead of
using the whole expression, i just want to use may be 1 or two
fields....let say in my output file i want to print just the $list
and $set or anyother two variables.


I meant that I wasn't sure of what's the field data and which the
separators are. Accordingly my suggestion above was a guess.

Since I'm still not sure, this is another guess:

split /[\s>]+/

-------------------------------------------

Hi,
For my second set of data:

abcd 4 {1,2,3} 3{1,2}
{3}
lmn- 3 {12,18,19,22} 4{12}
{18,19}
{19,22}


in the first line the delimiters are "space", and I could split the
fields in this line but the fields in the second line, I am unable to
access them or even split them from the first line or otherwise.

Thanks for all your help...waiting for reply!
~uc_sk
Jul 19 '05 #5
uc_sk wrote:
For my second set of data:

abcd 4 {1,2,3} 3
{1,2}
{3}


lmn- 3 {12,18,19,22} 4
{12}
{18,19}
{19,22}


in the first line the delimiters are "space", and I could split the
fields in this line but the fields in the second line, I am unable
to access them or even split them from the first line or otherwise.


Maybe I'm stupid, but I still don't understand what you mean. Please
post a *short* but *complete* program that people can copy and run,
and that illustrates what it is you are trying to do.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #6
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<qy********************@newsb.telia.net>...
uc_sk wrote:
For my second set of data:

abcd 4 {1,2,3} 3
{1,2}
{3}
lmn- 3 {12,18,19,22} 4
{12}
{18,19}
{19,22}


in the first line the delimiters are "space", and I could split the
fields in this line but the fields in the second line, I am unable
to access them or even split them from the first line or otherwise.


Maybe I'm stupid, but I still don't understand what you mean. Please
post a *short* but *complete* program that people can copy and run,
and that illustrates what it is you are trying to do.


Hi Gunnar
I am extremely sorry for confusing you....but I am in a big trouble
right now. May be my problem is easy but because i am new to PERL, I
am making it complicated....Anyways.....

FILE1 looks like:

fhhh--bf-h-gcb 10 {30,31,} 2
^[efg]{1}[gh]{3}.{2}[abc]{1}[efg]{1}.{1}[gh]{1}.{1}[fg]{1}[bcd]{1}[abc]{1}
(1,2,3,4,7,8,10,12,13,14,) 10 {23,27,28,30,31,36,} 6

fggg-ca-g--g-b 9 {24,27,36,} 3
^[efg]{1}[fg]{3}.{1}[bcd]{1}[ab]{1}.{1}[fg]{1}.{2}[fg]{1}.{1}[abc]{1}
(1,2,3,4,6,7,9,12,14,) 9 {17,22,24,27,36,38,} 6

I have given just 2 expressions, each has 9 fields and separated by
"space". So lets say the name of the fields are F1, F2, F3, F4, F5,
F6, F7, F8, F9.

FILE2 looks like:

^[efg]{1}[gh]{3}.{2}[abc]{1}[efg]{1}.{1}[gh]{1}.{1}[fg]{1}[bcd]{1}[abc]{1}
(1,2,3,4,7,8,10,12,13,14,) 10
*{23(5.3,7.7),27(8.8,8.4),28(5.8,6.8),30(5.0,6.8), 31(7.2,9.7),36(8.8,5.8),}
6 {23,28,30}
{27}
{31}
{36}
^[efg]{1}[fg]{3}.{1}[bcd]{1}[ab]{1}.{1}[fg]{1}.{2}[fg]{1}.{1}[abc]{1}
(1,2,3,4,6,7,9,12,14,) 9
*{17(1.9,6.8),22(5.1,7.4),24(5.5,8.7),27(8.8,8.4), 36(8.8,5.8),38(8.2,3.9),}
6 {17}
{22,24}
{27}
{36,38}


In this file we again have 2 expressions....but some fields are
different from the ones in FILE1 and some fields which were in FILE1
are not even in FILE2.....so lets say the fields are F5, F6, F7, F88,
F9 (these fields are the same as the ones in FILE1 except F88, which
is different from F8 of FILE1 as i have added something in it). Ok, so
these 5 fields are separated by "space" but there are more fields in
FILE2 which are in teh next line and are separated by "next line tab",
lets name them as F10

So what i want in my output file is that i want to compare F5 from
both the files and see if they are same, then concatanate them.....SO
BASICALLY I WANT THE OUTPUT AS:

F1 F2 F3 F4
F5 F6 F7 F8 F9
F10

since F10 can be either of 2 lines or 3 lines or may be 10 so i
thought that i will store all of them in an array and then will
outputting, i can read one by one and print on separate lines....but
my problem is that how to go to next line i.e 2nd line of expression 1
in FILE2.

I HOPE WHATEVER I WROTE ABOVE MAKES SOME SENSE AND I AM NOT MAKING YOU
CONFUSE.....i have tried so many ways, that everthing is mixed up in
my mind. Apologize for the confusion and would really appreciate your
or anybody's help to take me out of this problem.

Thanks a ton
~uc_sk
Jul 19 '05 #7
I still don't see any code. Anyway, this might be a start as regards
your FILE2:
#!/usr/bin/perl
use strict;
use warnings;

my @file2;

open FH, 'FILE2.txt' or die $!
{
local $/ = '';
while (<FH>) {
my %tmp;
@tmp{ qw/F5 F6 F7 F8 F9 F10/ } = split /\s+/, $_, 6;
$tmp{F10} = [ split /\n/, $tmp{F10} ];
push @file2, \%tmp;
}
}
close FH;

for my $rec (1..@file2) {
print "Record $rec\n";
for ( qw/F5 F6 F7 F8 F9/ ) {
print "$_: $file2[$rec-1]{$_}\n";
}
print 'F10: ', ( join '; ', @{ $file2[$rec-1]{F10} } ), "\n";
print "\n";
}

__END__
That outputs:

Record 1
F5:
^[efg]{1}[gh]{3}.{2}[abc]{1}[efg]{1}.{1}[gh]{1}.{1}[fg]{1}[bcd]{1}[abc]{1}
F6: (1,2,3,4,7,8,10,12,13,14,)
F7: 10
F8:
*{23(5.3,7.7),27(8.8,8.4),28(5.8,6.8),30(5.0,6.8), 31(7.2,9.7),36(8.8,5.8),}
F9: 6
F10: > {23,28,30}; > {27}; > {31}; > {36}

Record 2
F5: ^[efg]{1}[fg]{3}.{1}[bcd]{1}[ab]{1}.{1}[fg]{1}.{2}[fg]{1}.{1}[abc]{1}
F6: (1,2,3,4,6,7,9,12,14,)
F7: 9
F8:
*{17(1.9,6.8),22(5.1,7.4),24(5.5,8.7),27(8.8,8.4), 36(8.8,5.8),38(8.2,3.9),}
F9: 6
F10: > {17}; > {22,24}; > {27}; > {36,38}
HTH

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #8
uc_sk wrote:
FILE1 looks like:

fhhh--bf-h-gcb 10 {30,31,} 2
^[efg]{1}[gh]{3}.{2}[abc]{1}[efg]{1}.{1}[gh]{1}.{1}[fg]{1}[bcd]{1}[abc]{1}
(1,2,3,4,7,8,10,12,13,14,) 10 {23,27,28,30,31,36,} 6

fggg-ca-g--g-b 9 {24,27,36,} 3
^[efg]{1}[fg]{3}.{1}[bcd]{1}[ab]{1}.{1}[fg]{1}.{2}[fg]{1}.{1}[abc]{1}
(1,2,3,4,6,7,9,12,14,) 9 {17,22,24,27,36,38,} 6
That looks like records separated by blank lines.
If you use $/=""; perl will read the input in paragraph mode.
I have given just 2 expressions, each has 9 fields and separated by
"space". So lets say the name of the fields are F1, F2, F3, F4, F5,
F6, F7, F8, F9.
{ # Start of block for %info and %/
my %info;
local $/ = ''; # Set input separator for paragraph mode
open IN,'<',"file1" or die "Cannot read file1 - $!\n";
while (<IN>) { # Read until blank line
($F1,$F2,$F3,$F4,$F5,$F6,$F7,$F8,$F9) = split;
$info{$F5} = [ $F1,$F2,$F3,$F4,$F6,$F7,$F8,$F9 ]; # Save array in a hash
}; close IN;
FILE2 looks like:

^[efg]{1}[gh]{3}.{2}[abc]{1}[efg]{1}.{1}[gh]{1}.{1}[fg]{1}[bcd]{1}[abc]{1}
(1,2,3,4,7,8,10,12,13,14,) 10
*{23(5.3,7.7),27(8.8,8.4),28(5.8,6.8),30(5.0,6.8), 31(7.2,9.7),36(8.8,5.8),}
6
{23,28,30}
{27}
{31}
{36}

^[efg]{1}[fg]{3}.{1}[bcd]{1}[ab]{1}.{1}[fg]{1}.{2}[fg]{1}.{1}[abc]{1}
(1,2,3,4,6,7,9,12,14,) 9
*{17(1.9,6.8),22(5.1,7.4),24(5.5,8.7),27(8.8,8.4), 36(8.8,5.8),38(8.2,3.9),}
6
{17}
{22,24}
{27}
{36,38}

In this file we again have 2 expressions....but some fields are
different from the ones in FILE1 and some fields which were in FILE1
are not even in FILE2.....so lets say the fields are F5, F6, F7, F88,
F9 (these fields are the same as the ones in FILE1 except F88, which
is different from F8 of FILE1 as i have added something in it). Ok, so
these 5 fields are separated by "space" but there are more fields in
FILE2 which are in teh next line and are separated by "next line tab",
lets name them as F10


open IN,'<',"file2" or die "Cannot read file2 - $!\n";
while (<IN>) { # Read until blank line
($F5,$F6_,$F7_,$F8_,$F9_) = split;
$F10 = <IN>; # Next paragraph is F10
$F10 =~ s/\s*>//gs; # Make it look better (remove \n and '>')
So what i want in my output file is that i want to compare F5 from
both the files and see if they are same, then concatanate them.....SO
BASICALLY I WANT THE OUTPUT AS:

F1 F2 F3 F4
F5 F6 F7 F8 F9
F10
if (defined $info{$F5}) {
($F1,$F2,$F3,$F4,$F6,$F7,$F8,$F9) = @{$info{$F5}} # Get array from hash
warn "mismatch on F6" if $F6_ ne $F6;
warn "mismatch on F7" if $F7_ ne $F7; # $F8_ and $F8 are different
warn "mismatch on F9" if $F9_ ne $F9;
print "$F1 $F2 $F3 $F4\n$F5 $F6 $F7 $F8 $F9\n>$F10\n\n";
}
}; close IN;
} # End of block for %info and $/
I HOPE WHATEVER I WROTE ABOVE MAKES SOME SENSE AND I AM NOT MAKING YOU
CONFUSE.....i have tried so many ways, that everthing is mixed up in
my mind.


If the records are indeed separated by blank lines, then setting perl's
special variable $/ to "" makes it a lot easier, as shown above.

From the command line, use
perldoc perlvar
to discover other special perl variables. They are there to make your
life easier.
-Joe
Jul 19 '05 #9

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

Similar topics

3
by: Dr. Oz | last post by:
Hi, I am trying to read in a query string from one page and build a link to another page based on the query string. Here's the code I am using to read in the query string: <script...
1
by: trapeze.jsg | last post by:
Hi. I am trying to get through to Microsoft MapPoint Services using ZSI for soap handling. I can generate the service classes and also the soap-requests generated by the service classes seem to...
9
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the...
2
by: patrick beyries | last post by:
fair Warning - Admittedly I'm far from an access expert, but I'm not a beginner either. I want to split a value into multiple parts as delimited by a character - all as part of a query. For...
3
by: jwgoerlich | last post by:
Hello group, I am working on a query string class. The purpose is to parse name-value pairs from incoming text. Currently, I am using the Regex code below. I have two questions. First, the...
2
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
How can I run this query against a table in my Access database? I don't know hwo to use it in C#. In VB I would use .Recordset = "some sql statement". How do I do this in C#? //I get a vlaue...
2
by: I Hate My Computer | last post by:
I am using frames on a website. The title link on the title page adds a query string. The link goes to a page with two rows the second has two columns. I want the right column to be changed depending...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.