Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Matching values from array and finding length

Reply
Thread Tools

Matching values from array and finding length

 
 
sly
Guest
Posts: n/a
 
      09-07-2005
Hello

I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

Thanks all

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-07-2005
sly wrote:
> I have a list of values held in a .txt file, the image name and value 1
> are seperated by a tab.
>
> 14img.jpg 1
> 14img.jpg 1
> 14img.jpg 1
> 9img.jpg 1
> 4img.jpg 1
> 4img.jpg 1
> 7img.jpg 1
> 7img.jpg 1
>
> What I want to do is match the image names and push them onto a new
> array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
> etc


I don't understand the correlation between "4img.jpg" and "2". Is it
the number of digits that start the string?

What have you tried so far? What isn't working for you? With what
parts do you require assistance?

splitting the image name from the "value" of 1?
perldoc -f split

Capturing the starting digit(s)?
perldoc perlre
perldoc perlretut
perldoc perlreref

Finding the length of the captured starting digit(s)?
perldoc -f length

Once you've made an attempt, feel free to post what you have if it's
not working, and someone can probably help you fix it.

Paul Lalli

 
Reply With Quote
 
 
 
 
Ala Qumsieh
Guest
Posts: n/a
 
      09-07-2005
Paul Lalli wrote:
> sly wrote:
>
>>I have a list of values held in a .txt file, the image name and value 1
>>are seperated by a tab.
>>
>>14img.jpg 1
>>14img.jpg 1
>>14img.jpg 1
>>9img.jpg 1
>>4img.jpg 1
>>4img.jpg 1
>>7img.jpg 1
>>7img.jpg 1
>>
>>What I want to do is match the image names and push them onto a new
>>array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
>>etc

>
>
> I don't understand the correlation between "4img.jpg" and "2". Is it
> the number of digits that start the string?


Seems to be the number of times the string '4img.jpg' appears in his txt
file. But, I could be wrong.

--Ala
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      09-07-2005
In article <(E-Mail Removed) .com>,
"sly" <(E-Mail Removed)> wrote:

> Hello
>
> I have a list of values held in a .txt file, the image name and value 1
> are seperated by a tab.
>
> 14img.jpg 1
> 14img.jpg 1
> 14img.jpg 1
> 9img.jpg 1
> 4img.jpg 1
> 4img.jpg 1
> 7img.jpg 1
> 7img.jpg 1
>
> What I want to do is match the image names and push them onto a new
> array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
> etc


#!/usr/bin/perl
use strict;
use warnings;
my %img_array;
while (<DATA>){
chomp;
my ($img, $nr) = split /\s+/;
$img_array{$img} += $nr;
}
foreach (keys %img_array){
print "$_: $img_array{$_}\n";
}
__DATA__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1


OUTPUT:

7img.jpg: 2
4img.jpg: 2
9img.jpg: 1
14img.jpg: 3

NOTE:

I split on /\s+/, which is one or more whitespaces (tab, space, etc) which
works better with my example sent through usenet that expands my tabs to
spaces. You could of course change that to /\t/.


--
Sandman[.net]
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      09-07-2005
In article <(E-Mail Removed) .com>,
"Paul Lalli" <(E-Mail Removed)> wrote:

> sly wrote:
> > I have a list of values held in a .txt file, the image name and value 1
> > are seperated by a tab.
> >
> > 14img.jpg 1
> > 14img.jpg 1
> > 14img.jpg 1
> > 9img.jpg 1
> > 4img.jpg 1
> > 4img.jpg 1
> > 7img.jpg 1
> > 7img.jpg 1
> >
> > What I want to do is match the image names and push them onto a new
> > array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
> > etc

>
> I don't understand the correlation between "4img.jpg" and "2". Is it
> the number of digits that start the string?
>
> What have you tried so far? What isn't working for you? With what
> parts do you require assistance?
>
> splitting the image name from the "value" of 1?
> perldoc -f split
>
> Capturing the starting digit(s)?
> perldoc perlre
> perldoc perlretut
> perldoc perlreref
>
> Finding the length of the captured starting digit(s)?
> perldoc -f length
>
> Once you've made an attempt, feel free to post what you have if it's
> not working, and someone can probably help you fix it.


I think what the OP wanted to do was make the image list unique to the image
names, with the numbers added up for each image so that:

14img.jpg 1
14img.jpg 1
14img.jpg 1

Became

14img.jpg 3




--
Sandman[.net]
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-07-2005

sly wrote:
> Hello
>
> I have a list of values held in a .txt file, the image name and value 1
> are seperated by a tab.
>
> 14img.jpg 1
> 14img.jpg 1
> 14img.jpg 1
> 9img.jpg 1
> 4img.jpg 1
> 4img.jpg 1
> 7img.jpg 1
> 7img.jpg 1
>
> What I want to do is match the image names and push them onto a new
> array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
> etc
>
> Thanks all


In Ruby:

h = {}; array = DATA.to_a
array.each{|x|
if !h.key?(x)
puts "#{x.sub(/\s.*/m, "")} #{array.grep(x).size}"
h[x]=1
end
}

__END__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-08-2005

William James wrote:

> In Ruby:
>
> h = {}; array = DATA.to_a
> array.each{|x|
> if !h.key?(x)
> puts "#{x.sub(/\s.*/m, "")} #{array.grep(x).size}"
> h[x]=1
> end
> }
>
> __END__
> 14img.jpg 1
> 14img.jpg 1
> 14img.jpg 1
> 9img.jpg 1
> 4img.jpg 1
> 4img.jpg 1
> 7img.jpg 1
> 7img.jpg 1


h = {}; array = DATA.to_a
array.each{|x|
if !h.key?(x)
puts "#{x[/\w+/]} #{array.grep(x).size}"
h[x]=1
end
}

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-08-2005
sly wrote:
>
> I have a list of values held in a .txt file, the image name and value 1
> are seperated by a tab.
>
> 14img.jpg 1
> 14img.jpg 1
> 14img.jpg 1
> 9img.jpg 1
> 4img.jpg 1
> 4img.jpg 1
> 7img.jpg 1
> 7img.jpg 1
>
> What I want to do is match the image names and push them onto a new
> array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
> etc


#!/usr/bin/perl
use warnings;
use strict;

my %data;
while ( <DATA> ) {
my ( $key ) = split or next;
if ( %data && !exists $data{ $key } ) {
print join( "\t", %data ), "\n";
%data = ( $key, 0 );
}
$data{ $key }++;
}
print join( "\t", %data ), "\n";

__DATA__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
sly
Guest
Posts: n/a
 
      09-08-2005
Thanks to to everyone who replied on this.

Have this now working using the following:

my %img_array;

open(ODB, "<votecount_data.txt") || Error ('open','file');

while (<ODB>){
chomp;
my ($img, $nr) = split /\t/;
$img_array{$img} += $nr;
}

close(ODB);

mime();

foreach (keys %img_array){

print qq ($_: $img_array{$_}<br />\n);

}

I am getting a werid : 0 printed out as well but not too worried about
this.

Can someone explain this line to me please:

$img_array{$img} += $nr;

what is {$img} doing ?

What is the best way to now compare the results and find the higest ? I
normally just use if etc with constants to test conditions but don't
know how to do this for variables:

if ($img_array{$_} > $img_array{$_}) { }; ?????

Thanks again.

 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      09-08-2005

sly wrote:
> Thanks to to everyone who replied on this.
>
> Have this now working using the following:
>
> my %img_array;
>
> open(ODB, "<votecount_data.txt") || Error ('open','file');
>
> while (<ODB>){
> chomp;
> my ($img, $nr) = split /\t/;
> $img_array{$img} += $nr;
> }
>
> close(ODB);
>
> mime();
>
> foreach (keys %img_array){
>
> print qq ($_: $img_array{$_}<br />\n);
>
> }
>
> I am getting a werid : 0 printed out as well but not too worried about
> this.


That means there's a blank line in your input. With
'use strict;use warnings' you'd see output clues.

>
> Can someone explain this line to me please:
>
> $img_array{$img} += $nr;
>
> what is {$img} doing ?


creates/increments a hash entry with the results of the split.
>
> What is the best way to now compare the results and find the higest ? I
> normally just use if etc with constants to test conditions but don't
> know how to do this for variables:
>
> if ($img_array{$_} > $img_array{$_}) { }; ?????


I think you may want sort (perldoc -f sort):

foreach ( sort {$img_array{$b} <=> $img_array{$a} } keys %img_array ){

hth,
--
Charles DeRykus

 
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
Re: finding array length WANG Cong C++ 3 02-08-2009 10:23 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Matching array index to values retrieved from database John Wilson ASP General 2 07-16-2003 04:17 AM
Finding the non-matching values collection/array Robb Meade ASP General 4 06-29-2003 05:42 PM



Advertisments