Perl >> Query on Split

by sudhindra_k_s » Thu, 10 Jun 2004 14:52:10 GMT


Hi

I have a file with lines as shown below

//abc/... - //xyz/...
//abc1/... - //xyz1/...

Now i want to split the lines at "-" and get the string on the left in one array and the string on the right in another array.

ie: array1 = (//abc, //abc1) and array2 = (//xyz, //xyz1).

How do i do this?

Thanks
Sudhindra




Perl >> Query on Split

by prasanna.kothari » Thu, 10 Jun 2004 15:14:35 GMT


have a look at Split function:

http://www.perldoc.com/perl5.8.4/pod/func/split.html







Perl >> Query on Split

by rob » Thu, 10 Jun 2004 20:49:38 GMT




Names are useful handles to think about things with. By abstracting as far as
you have, with abc, xyz, @array1, @array2 and so on, you've thrown away the
clues that would help us to help you. Intriguingly you've left in the double and
single slashes that make this look like you could be dealing with URLs?

Anyway, your data looks like

<label> - <information>

without knowing more, and assuming that neither the <label> nor the
<information> contains whitespace, I'd write:

use strict;
use warnings;

open FILE, 'file.txt' or die $!;

my @array1;
my @array2;

while (<FILE>) {

my ($label, $hyphen, $info) = split;

die unless $hyphen eq '-';

push @array1, $label;
push @array2, $info;
}

HTH,

Rob




Query on Split

by krahnj » Fri, 11 Jun 2004 19:09:20 GMT




Hello,


my ( @array1, @array2 );
my $save_flush = $|;
$| = 0;
while ( <FILE> ) {
push @{ --$| ? \@array1 : \@array2 }, $_ for split /\s*-\s*/;
}
$| = $save_flush;



John
--
use Perl;
program
fulfillment


Query on Split

by rob » Sat, 12 Jun 2004 20:33:28 GMT





Eek! You're joking. Surely. Tell us! I'm still searching my brain
for a good reason to misuse the flush toggle like this, but if it
were sheer obfuscation then why push and pop the value of the thing?

Why not just

{
local $| = 0;

while (<FILE>) {
push @{ --$| ? \@array1 : \@array2 }, $_ foreach split /\s*-\s*/;
}
}

Tell me. Please. Before I die. Soon.

Rob







Query on Split

by ml-perl » Sat, 12 Jun 2004 20:52:40 GMT







That's an old idiom that appears in the FAQ and/or Perl Cookbook IIRC. A
much better idiom is to use the ! (not) operator on a regular my variable:

my $flag = 0;
while ( <FILE> ) {
push @{ $flag = !$flag ? \@array1 : \@array2 }, $_
for split /\s*-\s*/;
}




Similar Threads

1. split delimiters query

Am I right in thinking that if you double quote the seperator in split
the seperator is added to the array ie:

@array3=split(/"\t/",$value4); would add \t to the end of @array3 while

@array3=split(/\t/,$value4); would not

2. problem with whitespace not splitting on split. - Perl

3. split value when there's nothing to split

The value of $string could include dashes or not.  If it is, I'd like
the value of $foo to be set to the portion of the string to the left
of the first dash.  If not, I'd like the value of $foo to be null.

I'm doing the following, but $foo is equal to "1" if there are no
dashes in the string:

$foo = (split('-',$string)),[0];

Can anyone tell me why this is happening?

- Grant

4. Properly calling a parametrized query from another query

5. Any module to allow "query of queries" functionality?

Over the past few months I've been working on a project in ColdFusion.
In ColdFusion, database access is handled through the cfquery tag.
Here's a simple example:

<cfquery name="my_select" datasource="datasourcename">
SELECT my_col1, my_col2 FROM my_table;
</cfquery>

You can then access the result set from this query through the
my_select variable.

One feature that I really like is the "query of queries" feature.
Basically, a query of queries allows you to treat previously executed
queries as tables in a database.  For example, suppose that you have
already executed queries named "my_select" and "my_other_select":

<cfquery name="my_query_of_queries" dbtype="query">
SELECT my_col1, my_col3 FROM my_select, my_other_select
WHERE my_select.id=my_other_select.id
</cfquery>

This provides at least three benefits:

1.  You can join tables from different databases.

2.  You can reduce the number of calls sent to the database if you need
to retrieve similar information at different points in your
application.

3.  You can simplify queries that are difficult to understand (or
impossible to write) by using the query of queries to construct one-off
views of the database.  This is especially useful if you're working in
a situation where you don't have the ability to modify the database
schema.

Does anyone know of a Perl module that provides similar functionality?
DBD::AnyData is the closest thing that I could find.

--TWH

6. confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ]) - Perl

7. Replying to the perl list (was problem with whitespace not splitting on split.)

Chad Perrin wrote:
> On Thu, Apr 27, 2006 at 08:42:51AM -0500, Rance Hall wrote:
> 
>>some mail readers are probably capable of reading the list header and 
>>figuring it out, but I bet others don't (like mine)
> 
> 
> I use Mutt, which allows me to list-reply, but without setting up custom
> configurations it doesn't use list-reply without being specifically told
> to do so.  As such, I either give it a custom configuration, or I have
> to use a different method of replying than the standard reply command
> (using the r key for replies) if I want it to go to the list.  I'll
> probably set up custom config for it to check for list-reply first,
> though then I have to figure out how to make it easy for me to skip to
> individual replying if/when I want to do so without having to use a
> group reply and delete extra recipients (such as the list and myself).
> Not all that big a hardship, but it seems like kind of a suboptimal way
> to run a mailing list.
> 
> Er, that was more than I intended to say on the subject.
> 
> By checking the headers, I see that you're using Thunderbird.  I'm
> surprised that it doesn't support list-reply.  Are you saying that it
> doesn't recognize the list-reply header at all, or that it doesn't
> default to list-reply when it exists, or something else entirely?
> 


What I'm saying is this:  (and since we're on the subject, I'm going to 
add a suggestion)

What I've noticed in Thunderbird are a combination of things:

There is a bug in thunderbird where if you show all headers you don't 
actually see ALL of them, the panel where the headers are shown expands 
to the full size of the app window, but does not add a scroll bar to see 
whats left down the page, and unless your monitor is tall enough to show 
all headers (I've not see one that is) you won't actually see all of 
them.  I have reported this bug, but you need to know that what I'm 
saying is being filtered through this current limitation.


<begin humble suggestion>
1.  all of my other mailing lists have a subject rewrite that adds 
[perl-beginner] or something similar to the beginning of the subject if 
it is a regular mailing list mail and [perl-beginner-admin] or something 
if it is from a list administrator.

this makes filtering on the subject line compliment filtering on the 
from address.

I do this on all my other lists.  the from address, and the subject 
contains rules work very well together

I'm suggesting that the perl list adopt such a practice, sure would help 
me, and probably others as well.

<end suggestion>


Thunderbird, so far as I can tell offers two reply options when reading 
a message.  Reply, and Reply to ALL.


Which means "List Reply" is not a specific option per se.

If you "reply" to a message it goes to the original author of the question.

If you "reply to all" to a message the reply goes to the original author 
of the message with a cc to the list.

So for this list I have to reply to all, and edit the headers.

All of my other lists behave in the reverse, if you reply, it goes to 
the list, if you reply all, a second copy goes to the individual.

And I think this explains why my questions get answered twice by most of 
the list responders, they did a "reply to all" and I got a copy because 
my name was on the To: and I got a second copy because I'm a member of 
the list.

I'd like to see the perl lists do some sort of a header modification so 
the reply button works to reply to the address who sent you mail, which 
is the list and not the original author.

Just my $.02  after all, I joined this list because I'm a perl beginner 
(I'm not a rank noob in general, but my perl experience is extremely 
limited since php came out and that became my web programming language 
of choice.

but Ive got two or three projects that are going to need to be perl 
simply because thats what the intended audience is going to expect.

So here I am, starting over, again.  Common theme in this business.

Thanks for being willing to "help a guy out"

-- 
Rance Hall
System Administrator
Nebraska Turkey Growers
1-308-468-5711, ext. 106
 XXXX@XXXXX.COM 


8. problem with whitespace not splitting on split. -SOLVED - with followup - Perl