Matching values from array and finding length

Discussion in 'Perl Misc' started by sly, Sep 7, 2005.

  1. sly

    sly Guest

    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
    sly, Sep 7, 2005
    #1
    1. Advertising

  2. sly

    Paul Lalli Guest

    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
    Paul Lalli, Sep 7, 2005
    #2
    1. Advertising

  3. sly

    Ala Qumsieh Guest

    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
    Ala Qumsieh, Sep 7, 2005
    #3
  4. sly

    Sandman Guest

    In article <>,
    "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


    #!/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]
    Sandman, Sep 7, 2005
    #4
  5. sly

    Sandman Guest

    In article <>,
    "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?
    >
    > 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]
    Sandman, Sep 7, 2005
    #5
  6. 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
    William James, Sep 7, 2005
    #6
  7. 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
    }
    William James, Sep 8, 2005
    #7
  8. 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
    John W. Krahn, Sep 8, 2005
    #8
  9. sly

    sly Guest

    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.
    sly, Sep 8, 2005
    #9
  10. sly

    Guest

    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
    , Sep 8, 2005
    #10
  11. sly

    Anno Siegel Guest

    Sandman <> wrote in comp.lang.perl.misc:
    > In article <>,
    > "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

    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > my %img_array;
    > while (<DATA>){
    > chomp;


    No need to chomp. split on whitespace removes linefeeds.

    > 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


    A more compact way to do the same thing:

    ++ $img_array{ $_} for map +( split)[ 0], <DATA>;
    print "$_: $img_array{ $_}\n" for keys %img_array;

    It slurps DATA, so for very long lists the for loop is better.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 15, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WANG Cong

    Re: finding array length

    WANG Cong, Feb 7, 2009, in forum: C++
    Replies:
    3
    Views:
    357
    James Kanze
    Feb 8, 2009
  2. Robb Meade
    Replies:
    4
    Views:
    114
    Evertjan.
    Jun 29, 2003
  3. Kevin
    Replies:
    7
    Views:
    174
    Kevin
    May 17, 2011
  4. Tom
    Replies:
    3
    Views:
    196
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    461
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page