Click here to get back home

One of thos d'uh moments

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
One of thos d'uh moments Bill H 06-01-2008
Posted by Bill H on June 1, 2008, 9:40 am
Please log in for more thread options
For years I have always done various versions the following (and I
know there is probably some perlish way of doing it better, but old
habits die hard) to go through a list of data and build a string that
contains only certain matched data:

$stuff = "":
foreach $temp (@dbf)
{
@rbf = split(/\t/,$temp);
if($rbf[0] == 0)
{
$stuff .= $temp."|";
}
}
$stuff = substr($stuff,0,length($stuff) - 1);

But I just realized about 5 minutes ago instead of pulling off the
last usless "|" I could just replace $stuff = ""; with stuff = "|";
and the last line with: $stuff = substr($stuff,1);

And, while typing this I just realized I could do the above even
better:

foreach $temp (@dbf)
{
@rbf = split(/\t/,$temp);
if($rbf[0] == 0)
{
$cbf[@cbf] = $temp
}
}
$stuff = join('|',@cbf);

I am willing to bet now there is a one line command that would do all
this.

Bill H

Posted by Bill H on June 1, 2008, 9:41 am
Please log in for more thread options
> For years I have always done various versions the following (and I
> know there is probably some perlish way of doing it better, but old
> habits die hard) to go through a list of data and build a string that
> contains only certain matched data:
>
> $stuff =3D "":
> foreach $temp (@dbf)
> {
> @rbf =3D split(/\t/,$temp);
> if($rbf[0] =3D=3D 0)
> {
> $stuff .=3D $temp."|";}
> }
>
> $stuff =3D substr($stuff,0,length($stuff) - 1);
>
> But I just realized about 5 minutes ago instead of pulling off the
> last usless "|" I could just replace $stuff =3D ""; with stuff =3D "|";
> and the last line with: $stuff =3D substr($stuff,1);
>
> And, while typing this I just realized I could do the above even
> better:
>
> foreach $temp (@dbf)
> {
> @rbf =3D split(/\t/,$temp);
> if($rbf[0] =3D=3D 0)
> {
> $cbf[@cbf] =3D $temp}
> }
>
> $stuff =3D join('|',@cbf);
>
> I am willing to bet now there is a one line command that would do all
> this.
>
> Bill H

I can throw a sort in that join and make it better (oh this is getting
fun now)

$stuff =3D join('|',sort @cbf);

Bill H

Posted by Martijn Lievaart on June 1, 2008, 10:33 am
Please log in for more thread options
On Sun, 01 Jun 2008 06:40:07 -0700, Bill H wrote:

> I am willing to bet now there is a one line command that would do all
> this.

If I understand correctly what you are trying to do:

$stuff = join('|', grep(/^0\t/, @dbf));

HTH,
M4

Posted by Jürgen Exner on June 1, 2008, 11:15 am
Please log in for more thread options
>On Sun, 01 Jun 2008 06:40:07 -0700, Bill H wrote:
>
>> I am willing to bet now there is a one line command that would do all
>> this.
>
>If I understand correctly what you are trying to do:
>
>$stuff = join('|', grep(/^0\t/, @dbf));

He's looking for the numerical value 0, not the string '0' as the first
value each line.

jue

Posted by Martijn Lievaart on June 1, 2008, 12:03 pm
Please log in for more thread options
On Sun, 01 Jun 2008 15:15:59 +0000, Jürgen Exner wrote:

>>On Sun, 01 Jun 2008 06:40:07 -0700, Bill H wrote:
>>
>>> I am willing to bet now there is a one line command that would do all
>>> this.
>>
>>If I understand correctly what you are trying to do:
>>
>>$stuff = join('|', grep(/^0\t/, @dbf));
>
> He's looking for the numerical value 0, not the string '0' as the first
> value each line.

Yes, I realized that. However, in many (most) data the numerical value 0
is always the string '0'. It might be '0.00000', in which case my
solution does not work. It might be '', in which case the code is not
"use warnings;" safe.

If it really must be the same:

$stuff = join('|', grep((split(/\t/, $_))[0]==0, @dbf));

However that starts to border on the obscure. Lets try again:

sub starts_with_null {
        (split(/\t/, $_))[0]==0;
}

$stuff = join('|', grep(starts_with_null, @dbf));

Somewhat better. Or

$stuff = join('|', grep {
                        my ($t) = split(/\t/, $_);
                        $t == 0;
                        } @dbf);

Anyone has a more elegant solution? Must be possible.

M4


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap