Click here to get back home

Textfile to array or hash

 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
Textfile to array or hash J.Gluch 07-27-2008
Posted by J.Gluch on July 27, 2008, 8:20 am
Please log in for more thread options
Hello,

I want to extract data from a text file I recieve from the internet to
an array or hash. So far I did not found a solution via google and co
- maybe of my bad english and wrong search term.

The file is put into a varaible :
---------------------
$simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/
$m/similar.txt"`;
---------------------
and looks like this:
---------------------
100,,Medeski, Martin and Wood
69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
---------------------
I want to have either an array or hash that contains all artist names
(the string after the second comma, that have rating higher than 50
(the first number until first coma).
For this task I do not want to use any aditional modules. How do
process the data?

Juergen Gluch






Posted by Jens Thoms Toerring on July 27, 2008, 9:01 am
Please log in for more thread options
> I want to extract data from a text file I recieve from the internet to
> an array or hash. So far I did not found a solution via google and co
> - maybe of my bad english and wrong search term.

> The file is put into a varaible :
> ---------------------
> $simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/
> $m/similar.txt"`;
> ---------------------
> and looks like this:
> ---------------------
> 100,,Medeski, Martin and Wood
> 69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
> 49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
> 45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
> 43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
> 36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
> 30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
> 30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
> 29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
> 28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
> 27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
> ---------------------
> I want to have either an array or hash that contains all artist names
> (the string after the second comma, that have rating higher than 50
> (the first number until first coma).
> For this task I do not want to use any aditional modules. How do
> process the data?

As usual, there are lots of ways to skin the cat. This one is
hopefully easy to understand:

my @artists;
for my $line ( split /\n/, $similarArtistsAS ) {
my @data = split /,/, $line;
push @artists, $data[ 2 ] if $data[ 0 ] > 50;
}

The loop runs over all lines (splitting the content of '$similarArtistsAS'
up on the end-of-line character). Each line itself gets split up on the
commas. You push the third element of the result (the stuff after the
second comma) on the array if the first element of the result is larger
than 50.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by Bill H on July 27, 2008, 12:25 pm
Please log in for more thread options
On Jul 27, 9:01=A0am, j...@toerring.de (Jens Thoms Toerring) wrote:
> > I want to extract data from a text file I recieve from the internet to
> > an array or hash. So far I did not found a solution via google and co
> > - maybe of my bad english and wrong search term.
> > The file is put into a varaible :
> > ---------------------
> > $simartistsAS =3D `wget -q -O - "http://ws.audioscrobbler.com/1.0/artis=
t/
> > $m/similar.txt"`;
> > ---------------------
> > and looks like this:
> > ---------------------
> > 100,,Medeski, Martin and Wood
> > 69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
> > 49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
> > 45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
> > 43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
> > 36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
> > 30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
> > 30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
> > 29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
> > 28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
> > 27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
> > ---------------------
> > I want to have either an array or hash that contains all artist names
> > (the string after the second comma, that have rating higher than 50
> > (the first number until first coma).
> > For this task I do not want to use any aditional =A0modules. How do
> > process the data?
>
> As usual, there are lots of ways to skin the cat. This one is
> hopefully easy to understand:
>
> my @artists;
> for my $line ( split /\n/, $similarArtistsAS ) {
> =A0 =A0 my @data =3D split /,/, $line;
> =A0 =A0 push @artists, $data[ 2 ] if $data[ 0 ] > 50;
>
> }
>
> The loop runs over all lines (splitting the content of '$similarArtistsAS=
'
> up on the end-of-line character). Each line itself gets split up on the
> commas. You push the third element of the result (the stuff after the
> second comma) on the array if the first element of the result is larger
> than 50.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Regards, Jens
> --
> =A0 \ =A0 Jens Thoms Toerring =A0___ =A0 =A0 =A0j...@toerring.de
> =A0 =A0\__________________________ =A0 =A0 =A0http://toerring.de- Hide qu=
oted text -
>
> - Show quoted text -

I havent used push before, does it perform the same exact function as
using "=3D"? So in you sexample:

push @artists, $data[ 2 ] if $data[ 0 ] > 50;

Would this be the exact equivalent of doing:

if ($data[0] > 50){$artists[@artists] =3D $data[2];}

Or does more happen with push? Just for the record the last time I
ever used a "push" command was in Z80 ML programming on a Timex
Sinclair 1000 (bout 3 years ago).

Bill H

Posted by Jens Thoms Toerring on July 27, 2008, 1:29 pm
Please log in for more thread options
> I havent used push before, does it perform the same exact function as
> using "="? So in you sexample:

> push @artists, $data[ 2 ] if $data[ 0 ] > 50;

> Would this be the exact equivalent of doing:

> if ($data[0] > 50){$artists[@artists] = $data[2];}

Yes, here it does. It simply appends '$data[2]' to the end of
the '@artists' array. And it does it more efficiently (at
least that's what the documentation claims).

> Or does more happen with push?

Well, you can also 'push' a whole list, thus appending a number
of new elements to the array. So you can e.g. do

my @a = ( 1, 2, 3 );
my @b = ( 4, 5, 6 );
push @a, @b;

to concatenate @a and @b. And it returns the new number of
elements of the array.

> Just for the record the last time I
> ever used a "push" command was in Z80 ML programming on a Timex
> Sinclair 1000 (bout 3 years ago).

It's a bit similar as far as I remember (but it's nearly 20
years since I last used a Z80). There the PUSH command moved
the content of a register onto the stack, automatically de-
crementing the stack pointer by two (and POP moved data back
from the stack into a register and incremented SP twice).
Perl's push() and pop() functions allow you to use each array
as a kind of stack.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by Jürgen Exner on July 27, 2008, 1:49 pm
Please log in for more thread options
>I havent used push before, does it perform the same exact function as
>using "="?

Why don't you simply check the documentation that came with your Perl
installation?
        perldoc -f push

To answer your direct question: No.
"=" can be used to assign to any variable, push() is limited to arrays.

>So in you sexample:
>
>push @artists, $data[ 2 ] if $data[ 0 ] > 50;
>
>Would this be the exact equivalent of doing:
>
>if ($data[0] > 50){$artists[@artists] = $data[2];}

Yes, except for the return value of this construct.

>Or does more happen with push? Just for the record the last time I

Yes, ist also sets the return value to the new number of elements in the
array. This is explained in the documentation.

jue

Similar ThreadsPosted
Sort by hash vaule, an array of hash references October 6, 2005, 12:52 pm
How to use a textfile for filenames?? February 12, 2006, 11:16 pm
Need compress-zlib example for textfile into .gz July 20, 2006, 9:48 am
Perh Hash of Hash of Array structure March 29, 2007, 2:52 pm
Assigning a hash of array to another hash May 26, 2006, 8:17 pm
help about array and hash February 23, 2005, 3:30 am
array in hash June 8, 2006, 11:20 am
get Hash of Array September 25, 2007, 3:47 pm
A hash or array of regexp's? March 28, 2005, 7:31 am
Getting associative array from a hash July 7, 2005, 7:10 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap